Array operations on simulink math function for maximum efficiency
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Fangjun Jiang
2023-1-27
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 个评论
Fangjun Jiang
2023-1-27
编辑:Fangjun Jiang
2023-1-27
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).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulink Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!