Naming array with for loop

1 次查看(过去 30 天)
Afiye Havva Geçgel
编辑: 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 个评论
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