phobos
phobos

Reputation: 105

How to assign values to a variable iteratively using for loop in maxima?

I am using for loop to assign values to a variable, more precisely a matrix, in xmaxima. The values seem to get stored in the individual indices but I am unable to recall the entire matrix values by calling the matrix variable name.

For example:

Suppose I am assigning xreal[1]=5; xreal[2]=6;...... xreal[5]=9. I want xreal to have [5 6 7 8 9], which is not happening.

Below is my code. What's the right way to do this?

k:1;
for i from 1 thru length(xvals) do (
(if(imagpart(xvals[i]) = 0) then xreal[k]:xvals[i]),k:k+1
);

Here,

xreal[1]; gives 0.06111
xreal[2]; gives 0.080 and so on.

But xreal just gives "xreal" i.e. the variable name itself.

Upvotes: 1

Views: 1751

Answers (2)

Richard Fateman
Richard Fateman

Reputation: 266

Here's a simpler way.

makelist( if imagpart(xvals[s]) =0..... , i,1, length(xvals))

Upvotes: 1

soegaard
soegaard

Reputation: 31145

You must simply initialize the matrix first.

Example:

(%i1)  m:zeromatrix(1,3);
(%o1)  [0 0 0]

(%i2)  for i from 1 thru 3 do (
       m[1,i]: i);
       m;
(%o2)  [1 2 3]

Upvotes: 1

Related Questions