Function in a for loop

1 回表示 (過去 30 日間)
Tobias Dehn Nielsen
Tobias Dehn Nielsen 2021 年 11 月 1 日
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?

回答 (1 件)

Abhishek Kolla
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
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
Tobias Dehn Nielsen
Tobias Dehn Nielsen 2021 年 12 月 2 日
Thanks, helped a lot ;)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by