cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
hogi
Level XI

Matrix from variables

If I defined variables 

 

a=5;
b=3;

what is the fastest/most elegant way to get?

 

 

m = [5,3]

Is there something better than

m= Matrix(Eval({a,b}));

And why does

m=[Eval(a),Eval(b)]

not work?

Any links to related posts are welcome

 

6 REPLIES 6
txnelson
Super User

Re: Matrix from variables

For me, the easiest way to get to the result is

m=matrix(a) |/ matrix(b);
Jim
hogi
Level XI

Re: Matrix from variables

oh, Matrix evaluates the list? got it!

hogi_0-1674830940841.png


Then 

m= Matrix({a,b});

works as well

And what's the reason that

m=[Eval(a),Eval(b)]

doesn't work?

ErraticAttack
Level VI

Re: Matrix from variables

Using Matrix( {a, b} ) is probably the easiest way. One way that I use commonly is [](0,1) |/ a |/ b

As for your second question, matrices are special in JSL and are not evaluated as you would expect. Normally you can put a JSL expression anywhere in the JSL script and only the final argument would get returned. However, with matrices (probably for the code to be more optimized and speedy), you cannot put expressions in the matrix elements and expect that it will be populated with the number returned by that expression. Expressions are strictly _not_ allowed in matrices, only numbers are allowed. This is also why variables don't work within matrices, as variables only make sense when they are evaluated as an expression.

Jordan
hogi
Level XI

Re: Matrix from variables

OK understood.Thanks a lot for the explanation.


@ErraticAttack wrote:

you cannot put expressions in the matrix elements and expect that it will be populated with the number returned by that expression. 


Is a good wording
It could work but you cannot expect that it will work:

a=5;
matrix(a/b); // -> works
matrix(char(a)) // -> doesn't matrix({char(a)}) // --> doesn't (I hoped this could be a workaround ...)

So ..

@ErraticAttack wrote:

Expressions are strictly _not_ allowed in matrices, only numbers are allowed. 


seems to be slightly too strict - but a good approach "to be on the safe side"

 

hogi
Level XI

Re: Matrix from variables

It's amazing that 

[](0,1) |/ a |/ b

works. That means, it automatically evaluates the arguments and converts them into a matrix?

a= {{5,3}}
b = {{7 ,6}}
[](0,2) |/ a |/ b

 

While sharing my learning with my colleagues I noticed:

 

Up to now, I thought that [a, b] is just a short form of the constructor matrix({a,b}).
But it's the result, right?

Now I really understand what you mean::

@ErraticAttack wrote:

you cannot put expressions in the matrix elements

Then the examples in my last post don't make sense. They just don't work because the matrix() function doesn't have a function to set non-numerical values to "." (missing) - compare the original topic.

By the way:
I am a bit surprised to see the result of matrix(3). Kind of an easter egg ? 

hogi
Level XI

Re: Matrix from variables

What do you think about 

a=5;
b=3;
m=matrix(a) |/b  //-> works !?!

a combination of all riddles in one expression