Array operations on simulink math function for maximum efficiency
1 view (last 30 days)
Show older comments
Hello, I am developing a model on simulink. I use math fucntion blocks while developing the model. Repetitive processes such as creating arrays, transferring arrays between blocks are often dominated in the content of Math function blocks. I use a for loop when creating arrays and storing data in matrix content, but I noticed that the for loop blocks simulink blocks and slows down the simulation a lot. How can I use a function for the matrix structure in which recursive functions will occur? I'm open to all suggestions.. Waiting for your answers. Thanks.
Example code: what ı want to do ------ obtained f array (1,100)
m=10;
n=10;
k=1;
f=zeros(1,m*n);
for i=1:1:m*n
f(i)=x(k)+y(i)...;
if k==n
k=0;
end
k=1+k;
end
0 Comments
Accepted Answer
Fangjun Jiang
on 27 Jan 2023
Instead of using "For Iterator Subsystem" block and "Math Function" block, I would suggest using the "MATLAB Function" block.
You would write similar code like what you have in your post.
3 Comments
Fangjun Jiang
on 27 Jan 2023
Edited: Fangjun Jiang
on 27 Jan 2023
Then it depends on the needed processing task and the implementation code. You can do.
- Copy the MATLAB Function block code to a separate .m file and utilize the Profile Your Code to Improve Performance
- Similarly, use the Simulink Profile to see if the MATLAB Function block is the bottle neck.
- Some loop operation might be able to be replaced with matrix operation. Ask separate questions regarding those operations.
Also, the MATLAB Function block will be executed at every simulation step of the Simulink simulation. If the whole for-loop inside the MATLAB Function block only needs to be run once per simulation (for example, at the beginning of the simulation), then the MATLAB Function block needs to be put in a different place (e.g. inside an 'Initialize Function' block).
More Answers (0)
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!