Question

I had a complex problem which is solved. Now i would love to automate it. In doing so, I do address a position in a matrix. This positions does contain a variable. I want to assign a value to this variable, by its position in the matrix. Here is a simple example of what I want to do:

    (%i1) M:[a,b,c];
    (%o1) [a,b,c]
    (%i2) M[1];
    (%o2) a
    (%i3) ev(M[1]):3;
    (%o3) error: assignment: cannot assign to ev(M[1])
    (%i4) float(a);
    (%o4) a

I would love the variable "a" to be 3 now. But ev() is clearly not the right function for this.

My question is: is what i want even possible? does anyone know how to do? I looked the whole day and I am really depressed now :-(

Was it helpful?

Solution

I think what you want is the :: operator, which evaluates its left-hand side and assigns to that.

(%i1) M : [a, b, c] $
(%i2) M[1] :: 123 $
(%i3) a;
(%o3)      123

OTHER TIPS

To replace a in M with the value 3:

M: M, a=3;

Which is, of course, just a shorter version of writing:

M: ev(M, a=3);

Or simply use ev(M, a=3) instead of M to use M with a=3 once without changing M in memory.

To set the 1st element in M to 3:

M[1]: 3

To replace the variable stored in M[1] in all of M:

M: ev(M, M[1]=3);

I hope one of those answers your question..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top