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
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Rik
Rik 2020년 11월 13일

0 개 추천

A={[30;31],[],[];[18;20;24],[27;24;26],[];[13;16;18],[14;16;17],[15;16;16];5,9,8};
NewMatrix=flipud(A).';
NewMatrix=NewMatrix(:);
NewMatrix(cellfun('isempty',NewMatrix))=[];
celldisp(NewMatrix)
NewMatrix{1} = 5 NewMatrix{2} = 9 NewMatrix{3} = 8 NewMatrix{4} = 13 16 18 NewMatrix{5} = 14 16 17 NewMatrix{6} = 15 16 16 NewMatrix{7} = 18 20 24 NewMatrix{8} = 27 24 26 NewMatrix{9} = 30 31

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

SS
2020년 11월 13일

댓글:

SS
2020년 11월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by