Run the same code with multiple variables.

Hi!
I have 10-20 different matrices, each containing a set of coordinates (one longitude column & one latitude column). I wrote a code to calculate stuff for one matrix, but I want to loop the whole code for all of my matrices.
I need to compare the results from each matrix, so their results have to end up in a joint new matrix.
Thank you!

7 comentarios

Stephen23
Stephen23 el 19 de Feb. de 2018
Editada: Stephen23 el 19 de Feb. de 2018
"I want to loop the whole code for all of my matrices"
Put the matrices into one array (which might be numeric or cell) and use indexing: indexing is simple, neat, easy to debug, and very efficient. The best solution would be to avoid this problem by using one array right from the very start, so that you can write simpler, more efficient code.
You should read this:
It is possible to do what you asked for, but the MATLAB documentation states: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array"
Hampus Alfredsson
Hampus Alfredsson el 19 de Feb. de 2018
Thank you! I'm pretty new to Matlab, I believe an array is what I am looking for.
Stephen23
Stephen23 el 19 de Feb. de 2018
Editada: Stephen23 el 19 de Feb. de 2018
@Hampus Alfredsson: all numeric scalars/vectors/matrices are arrays in MATLAB, they are not a different class of variable (e.g. a scalar is just a 1x1x1x1x... array). The only thing is to use appropriate indexing:
Hampus Alfredsson
Hampus Alfredsson el 19 de Feb. de 2018
Editada: Hampus Alfredsson el 19 de Feb. de 2018
Ok! But also, my matrices are of different size but I still want to execute the code for each matrix. How can I store different sized matrices in a multidimensional array?
Walter Roberson
Walter Roberson el 19 de Feb. de 2018
To store different sizes of matrices in a multidimensional array, you need to either concatenate them along a dimension that is known to be the same for each, or else you need to pad the smaller ones to be consistent size. Some of the common padding values are 0, nan, inf, and -9999
Hampus Alfredsson
Hampus Alfredsson el 19 de Feb. de 2018
Ok! I will look into this, thank you @Walter Robertson.
Stephen23
Stephen23 el 20 de Feb. de 2018
"How can I store different sized matrices in a multidimensional array?"
Or, as an alternative, use one simple cell array.

Iniciar sesión para comentar.

 Respuesta aceptada

Jeff Miller
Jeff Miller el 19 de Feb. de 2018

0 votos

It might be convenient to store each of the 10-20 matrices as one cell in a cell array, since that doesn't require that they have the same size. Then, for each cell, call the code to process the one array in that cell.

1 comentario

Example:
% Create some test data:
C = cell(1, 20);
for k = 1:20
n = randi([1,5], 1, 2); % Some random dimensions
C{k} = rand(n(1), n(2)); % A random matrix
end
% Now process them in a loop:
for k = 1:numel(C)
C{k} = sin(C{k});
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 19 de Feb. de 2018

Comentada:

Jan
el 20 de Feb. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by