cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
samir
Level IV

How to get the median of an array

Hi,

I am looking for a simple thing: the median of an array.

I found complex things with tabulate, summary tables,...But I need simpler:

Suppose I have an array (Matrix) A:

A = 1::10;

How to get the media of A ??

 

 

Thank you.

5 REPLIES 5
stan_koprowski
Community Manager Community Manager

Re: How to get the median of an array

Hi @samir,

You can use the quantile function without and with a by variable as shown in the example below.

Names Default To Here( 1 );
Open( "$SAMPLE_DATA/Big Class.jmp" );
Row() = 1;
median_ht = Col Quantile( :height, 0.5 );
Row() = 1;
median_htbyage = Col Quantile( :height, 0.5, :age );
Show( median_ht ); //Show median height
Show( median_htbyage ); //Show median height by age



Col Median FunctionCol Median Function

cheers,

Stan

uday_guntupalli
Level VIII

Re: How to get the median of an array

@stan_koprowski
       While I thought of this, I think @samir was asking, how the same can be calculated for a matrix not from a data table ? I looked at the scripting index, there does not seem a direct way to compute the quantiles or median for a matrix (unless I missed it) 

Best
Uday
stan_koprowski
Community Manager Community Manager

Re: How to get the median of an array

Hi @uday_guntupalli,

You could do the same with a matrix as follows--

Names Default To Here( 1 );
Open( "$SAMPLE_DATA/Big Class.jmp" );
htmat = :height<<Get As Matrix;
htmat1 = 1 :: 10;
Eval List(
	{
	Quantile( 0.5, 0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 ), 
	Quantile(0.5,htmat),
	Quantile(0.5,htmat1)
	}
);

//output

{500, 63, 5.5}

 

cheers,

Stan

samir
Level IV

Re: How to get the median of an array

Thanks !

I guess @uday_guntupalli got my point :) and @stan_koprowski got a solution.

Indeed I was looking for a median of a matrix because I was not able to extract it immediatly from the table, but may be you can lead me to a way:

I hava a table (like below). I would like to calculate the median of each row in a simple way. Of course I need to exclude the 1st column from the calculation. In excel it is straight forward, but in JSL-JMP, I do not know how to do it simply (avoiding to stack the table,...).

Example of a table:

Consumption   Monday  Tuesday  Wednesday  Thursday  Friday  Saturday  Sunday  Median

Electricity         11.2        10.4         13.0              7.1            12.4     11.9         9.7         ?

Gas                  100.4      86.9         94.8              103.0        112.3    95.3        97.8       ?

Craige_Hales
Super User

Re: How to get the median of an array

Using Stan's quantile method, add the formula to the median column:

New Table( "Utility Data",
	New Column( "Consumption", Character, "Nominal", Set Values( {"Electricity", "Gas"} ) ),
	New Column( "Monday", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [11.2, 100.4] ) ),
	New Column( "Tuesday", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [10.4, 86.9] ) ),
	New Column( "Wednesday", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [13, 94.8] ) ),
	New Column( "Thursday", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [7.1, 103] ) ),
	New Column( "Friday", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [12.4, 112.3] ) ),
	New Column( "Saturday", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [11.9, 95.3] ) ),
	New Column( "Sunday", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [9.7, 97.8] ) ),
	New Column( "Median", Numeric, "Continuous", Format( "Best", 12 ),
	// the 2nd argument to the quantile function is a matrix made from the days of the row...
		formula( Quantile( 0.5, monday || tuesday || wednesday || thursday || friday || saturday || sunday ) )
	)
)

column formula using Quantile to get Mediancolumn formula using Quantile to get Median

Craige