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?

Réponses (1)

KALYAN ACHARJYA
KALYAN ACHARJYA le 22 Mai 2019
Modifié(e) : KALYAN ACHARJYA le 22 Mai 2019

1 vote

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 commentaires

madhan ravi
madhan ravi le 22 Mai 2019
Don’t forget to preallocate Kalyan!!
KALYAN ACHARJYA
KALYAN ACHARJYA le 22 Mai 2019
Yes again, I missed, is it OK @madhan?
Afiye Havva Geçgel
Afiye Havva Geçgel le 22 Mai 2019
Thanks for both of you
Stephen23
Stephen23 le 22 Mai 2019
Modifié(e) : Stephen23 le 22 Mai 2019
Note that numeric arrays should be preallocated before the loop, e.g.:
M = nan(1,n);
for k=1:n
M(k) = ...
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by