A Varying matrix name in loop

조회 수: 7 (최근 30일)
Ahmed Abdulla
Ahmed Abdulla 2020년 6월 8일
편집: Stephen23 2020년 6월 8일
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일
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일
N = 2; % example
matrix = cell(N,1);
for k = 1:N
matrix{k} = rand;
end
celldisp(matrix)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by