help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: append numbers to variable


From: Carlo de Falco
Subject: Re: append numbers to variable
Date: Thu, 7 Sep 2006 11:07:05 +0200

On 9/7/06, Gerald Ebberink <address@hidden> wrote:
something like this:
x=0;
for i=1:5
        command=sprintf("x%d=%d",i,i);
        eval(command);
endfor


This works, but I don't understand why you would prefer this over:

x0 = 0;
x   = x0 + [1:5];
x(1)
ans = 1
x(2)
ans = 2
x(3)
ans = 3
x(4)
ans = 4
x(5)
ans = 5

i.e. why would you create a set of variables with different names instead
of storing your values in a vector?
It is much more convenient to be able to access one of the values via

x(i)
rather than
eval(sprintf("x%d",i))

-
c.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]