Store all iterations in nested for loop
Afficher commentaires plus anciens

I basically want to store the numbers from A{} to NewMatrix{}. I want A to be a 9x1 matrix array.
num_states = [1 2 3 3 1]; %stating that the cell in A has either 1, 2, or 3 numbers.
In the end my New Matrix should be like this:
New Matrix{1,1} = A{4,1}
New Matrix {2,1} = A{4,2}
New Matrix(3,1} = A{4,3}
New Matrix(4,1} = A{3,1}
New Matrix(5,1} = A{3,2}
etc... but for some reason NewMatrix{:,1} = A{1,1}
If you could please help me understand how nested loops work and how to fix this problem? Please see below.
__________________________________________________________________________________________
for k =4:-1:1
for i=1:num_states(k)
for j= 1:9
NewMatrix{j,1} =A{k,i};
end
end
end
1 commentaire
Rik
le 13 Nov 2020
Your keep overwriting the data in your new matrix. Your description is not clear enough for me to suggest a fix. You can step through the code with the debugger to see what the problem is.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!