I have a loop running from i=1 to N where N is unkown and inputted by the user and the commands in the loop generates a matrix and i would like the first loop to give me matrix1 and the second loopd to give me matrix2 so the matrix name is changed based on the i value, but im not really sure how to go about this problem

1 件のコメント

Stephen23
Stephen23 2020 年 6 月 8 日
編集済み: Stephen23 2020 年 6 月 8 日
"... matrix1 ... matrix2 so the matrix name is changed based on the i value..."
The simple, efficient, and strongly recommended approach is just to use indexing, e.g. with a cell array:

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

 採用された回答

KSSV
KSSV 2020 年 6 月 8 日
編集済み: KSSV 2020 年 6 月 8 日

1 投票

N = 10;
matrix = cell(N,1) ;
for i = 1:N
matrix{i} = rand(3) ;
end
Trust me, there is no meaning in naming as matrix1, matrix2,........ rather you can use the above and call them by
matrix{1}
matrix{2}
This is called cell referencing. Read about cells.

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 6 月 8 日

1 投票

N = 2; % example
matrix = cell(N,1);
for k = 1:N
matrix{k} = rand;
end
celldisp(matrix)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2019b

質問済み:

2020 年 6 月 8 日

編集済み:

2020 年 6 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by