Share your ideas for the JMP Scripting Unsession at Discovery Summit by September 17th. We hope to see you there!
Choose Language Hide Translation Bar
Highlighted
kadiew
Level II

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

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)));
Learn it once, use it forever!

View solution in original post

4 REPLIES 4
Highlighted

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}
		)
	)
);
Learn it once, use it forever!
Highlighted
kadiew
Level II

Re: Find median of list

do you have any suggestions on how to use the variable in the function? My phiNew variable is an output from several other functions and I don't really have the option to specify the values inside the Quantile function. I've tried As List(phiNew) but I still get the same output
Highlighted

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)));
Learn it once, use it forever!

View solution in original post

Highlighted
Craige_Hales
Staff (Retired)

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!

Craige
Article Labels

    There are no labels assigned to this post.