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!