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
Super User

## Calculate a vector of miniums from a 2 dimentional matrix

I am looking for a simple method to take a 2xn matrix and product a vector where each element in the vector is the minimum value for each row in the 2xn matrix

``````a=[ 1 4, 3 2, 5 6]
// the final result needs to be
[1, 2, 5]``````

Looping through matrix "a" can easily do this, but......I am hoping someone out there is more fluent in matrices that may have a more efficient solution

Jim
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Super User

## Re: Calculate a vector of miniums from a 2 dimentional matrix

Hi Jim,

``````m = [1 4, 3 2, 5 6];

v = Transpose( V Min( Transpose( m ) ) );  // VMin( m`)` ``````
5 REPLIES 5
Highlighted
Super User

## Re: Calculate a vector of miniums from a 2 dimentional matrix

Hi Jim,

``````m = [1 4, 3 2, 5 6];

v = Transpose( V Min( Transpose( m ) ) );  // VMin( m`)` ``````
Highlighted
Super User

excellent...
Jim
Highlighted
Staff (Retired)

## Re: Calculate a vector of miniums from a 2 dimentional matrix

This set of functions all work on the vertical dimension of a matrix and can use the same technique to get a horizontal version.

Craige
Highlighted
Staff

## Re: Calculate a vector of miniums from a 2 dimentional matrix

I knew there was a function but I couldn't find it, so I did it step by step using matrix operations. Touché @gzmorgan0 !

``````Names Default to Here( 1 );

// define 2xn data matrix
a = [ 1 4, 3 2, 5 6];

// compute row-wise differences as a vector
b = a[0,1] - a[0,2];

// determine deficit in first column of data matrix
c = b < 0;

// determine deficit in second column of data matrix
d = !c;

// create conforming matrix of deficits
e = c || d;

// multiply to get minimum value
f = a * e`;

// extract minimum value from diagonal elements
Vec Diag( f );``````

I doubt that my way is more efficient than iterating over the rows of the data matrix. I am sure it is not more efficient than V Min()!

Learn it once, use it forever!
Highlighted
Staff (Retired)

## Re: Calculate a vector of miniums from a 2 dimentional matrix

Wow! I hope you are doing @DonMcCormack 's challenges! I'm still going to need to work through that a bit longer.

Craige
Article Labels