Function in a for loop

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?

Respuestas (1)

Abhishek Kolla
Abhishek Kolla el 5 de Nov. de 2021

0 votos

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 comentarios

Tobias Dehn Nielsen
Tobias Dehn Nielsen el 7 de Nov. de 2021
Thanks! My problem now is that the matrices are of different dimension, How do i solve that promblem? Is it possible to save the date on n different matrices?
Abhishek Kolla
Abhishek Kolla el 8 de Nov. de 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 el 2 de Dic. de 2021
Thanks, helped a lot ;)

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 1 de Nov. de 2021

Comentada:

el 2 de Dic. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by