Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- JMP User Community
- :
- Discussions
- :
- Re: Find median of list

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

May 29, 2020 11:57 AM
(783 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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!

4 REPLIES 4

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- 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}
)
)
);
```

Learn it once, use it forever!

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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!

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- 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!

Craige

Article Labels

There are no labels assigned to this post.