Run the same code with multiple variables.

2 vues (au cours des 30 derniers jours)
Hampus Alfredsson
Hampus Alfredsson le 19 Fév 2018
Commenté : Jan le 20 Fév 2018
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 commentaires
Hampus Alfredsson
Hampus Alfredsson le 19 Fév 2018
Ok! I will look into this, thank you @Walter Robertson.
Stephen23
Stephen23 le 20 Fév 2018
"How can I store different sized matrices in a multidimensional array?"
Or, as an alternative, use one simple cell array.

Connectez-vous pour commenter.

Réponse acceptée

Jeff Miller
Jeff Miller le 19 Fév 2018
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 commentaire
Jan
Jan le 20 Fév 2018
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by