- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Matrix from variables
For me, the easiest way to get to the result is
m=matrix(a) |/ matrix(b);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Matrix from variables
oh, Matrix evaluates the list? got it!
Then
m= Matrix({a,b});
works as well
And what's the reason that
m=[Eval(a),Eval(b)]
doesn't work?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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); // -> worksmatrix(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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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