- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Find median of list
I'm looking to evaluate the median of my list:
{[-0.368200035870304], [0.049749270873994], [0.612391979268901], [1.15772787379131], [-0.10485383375963]}
using the following code:
print(Eval List (Quantile (0.5, phiNew)));
where phiNew is the name where the list is held. The output results in ".". Any idea why I can't get the median of my list to work properly?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Find median of list
I see. This script shows all the steps starting with the result returned to you from another function. The steps could be combined by nesting functions to make the code more concise, but I wanted the reasoning to be clear.
Names Default to Here( 1 );
result = {[-0.368200035870304], [0.049749270873994], [0.612391979268901], [1.15772787379131], [-0.10485383375963]};
result = Char( result );
result = Substitute( result,
"[", "",
"]", ""
);
result = Parse( result );
print(Eval List (Quantile (0.5, result)));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Find median of list
That is not a list of numbers. That is a list of 1x1 matrices. What is the median of a list of matrices? Try this way instead:
Print(
Eval List(
Quantile(
0.5,
{-0.368200035870304, 0.049749270873994, 0.612391979268901, 1.15772787379131, -0.10485383375963}
)
)
);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Find median of list
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Find median of list
I see. This script shows all the steps starting with the result returned to you from another function. The steps could be combined by nesting functions to make the code more concise, but I wanted the reasoning to be clear.
Names Default to Here( 1 );
result = {[-0.368200035870304], [0.049749270873994], [0.612391979268901], [1.15772787379131], [-0.10485383375963]};
result = Char( result );
result = Substitute( result,
"[", "",
"]", ""
);
result = Parse( result );
print(Eval List (Quantile (0.5, result)));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
Re: Find median of list
You can avoid converting the numbers to character and back like this:
result = {[-0.368200035870304], [0.049749270873994], [0.612391979268901], [1.15772787379131], [-0.10485383375963]};
Substitute Into( result, Expr( {} ), Expr( V Concat() ) ); // make a col vector
result = Eval( result );
Show( result );
//result = [-0.368200035870304, 0.049749270873994, 0.612391979268901, 1.15772787379131, -0.10485383375963];
// or
result = {[-0.368200035870304], [0.049749270873994], [0.612391979268901], [1.15772787379131], [-0.10485383375963]};
Substitute Into( result, Expr( {} ), Expr( Concat() ) ); // make a row vector
result = Eval( result );
Show( result );
// result = [-0.368200035870304 0.049749270873994 0.612391979268901 1.15772787379131 -0.10485383375963];
The Vconcat or Concat functions concatenate matrices, vertically or horizontally. The SubstituteInto function operates on the expressions, converting the curly braces into a VConcat or Concat operator. If you look at result after the substitute, before the eval, you'll see it is still an expression:
Show( result );
/*:
result = [-0.368200035870304] |/ [0.049749270873994] |/ [0.612391979268901] |/ [1.15772787379131] |/ [-0.10485383375963];
where |/ is the VConcat operator. After the Eval, it is a matrix as shown in the comment. The matrix will be a col or row vector depending which concatenation direction is used.
Whichever approach you take, leave a comment in your JSL about what it does and why!