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

1 件のコメント

SS
SS 2020 年 11 月 14 日
Thank you. This worked!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops 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