Naming array with for loop

조회 수: 1 (최근 30일)
Afiye Havva Geçgel
Afiye Havva Geçgel 2019년 5월 22일
편집: Stephen23 2019년 5월 22일
for i=1:n
M=input('Enter array')
end
I see the result as,
M= 1 2 3
M= 3 2 4 and so on.
But i want to see that
M1= 1 2 3
M2= 3 2 4 and so on
How can i fixed it?

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 5월 22일
편집: KALYAN ACHARJYA 2019년 5월 22일
Better to go for M{1},M{2}.....as a cell array, here M{1}, M{2} are arrays having any size
M=cell(1,n)
for i=1:n
M{i}=....
end
if M1 and M2 are scalars then
M=[];
for i=1:n
M(i)=....
end
More discussion on M1, M2 like variables, read here
  댓글 수: 4
Afiye Havva Geçgel
Afiye Havva Geçgel 2019년 5월 22일
Thanks for both of you
Stephen23
Stephen23 2019년 5월 22일
편집: Stephen23 2019년 5월 22일
Note that numeric arrays should be preallocated before the loop, e.g.:
M = nan(1,n);
for k=1:n
M(k) = ...
end

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by