Store all iterations in nested for loop
显示 更早的评论

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 个评论
Rik
2020-11-13
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!