variable name by cancatation in matlab

1 Ansicht (letzte 30 Tage)
suvadip paul
suvadip paul am 7 Aug. 2014
Kommentiert: Image Analyst am 7 Aug. 2014
I want to set
c1=1;
c2=4;
c3=9;
c4=16;
How to do this in the following way
for i=1:4
ci=i^2;
end
i.e i will vary to generate c1, c2, c3, c4 etc. from ci.

Antworten (1)

Image Analyst
Image Analyst am 7 Aug. 2014
Bearbeitet: Image Analyst am 7 Aug. 2014
Don't use separate variables with different names. Use an array:
ci = [1:4].^2
If you really want to do it the for loop way, do this:
for k = 1 : 4
c(k) = k ^2;
end
  2 Kommentare
suvadip paul
suvadip paul am 7 Aug. 2014
Actually my actual problem is another one. I have just given an example. I just want to know how to change the variable name through a loop.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by