Function in a for loop
1 次查看(过去 30 天)
显示 更早的评论
Hey,
I have created a function where the output i a matrix.
I want to create a for loop thats creates n different matrices based on that function. How do i do that?
0 个评论
回答(1 个)
Abhishek Kolla
2021-11-5
The code snippet below should solve your question
for i=1:n
A(:,:,i)=fn();
end
where A is 3D array. More info on higher dimensional arrays can be found here Multidimensional Arrays - MATLAB & Simulink (mathworks.com)
3 个评论
Abhishek Kolla
2021-11-8
If n is small then a simple way is to define each matrix seperately and assign the function output, otherwise the above code should work for different dimensions
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!