Function in a for loop

1 Ansicht (letzte 30 Tage)
Tobias Dehn Nielsen
Tobias Dehn Nielsen am 1 Nov. 2021
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?

Antworten (1)

Abhishek Kolla
Abhishek Kolla am 5 Nov. 2021
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 Kommentare
Abhishek Kolla
Abhishek Kolla am 8 Nov. 2021
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 am 2 Dez. 2021
Thanks, helped a lot ;)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by