cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
0 Kudos

Ability to sort a matrix and retain the indices

All, 
   When you have a matrix and you would like to sort the matrix, I am currently unaware of a function that would let me sort the matrix without using a loop over the columns or rows. I don't think this is particularly efficient. Additionally, there is no straight forward way to retain the positions once sorting is performed. Please see the following discussion for more details : 

https://community.jmp.com/t5/Discussions/Sort-a-matrix/m-p/85678#M38246 

 

   As you will see in the discussion , @Mark_Bailey was kind enough to offer a simple custom function, which I would think is useful to have in the JMP arsenal. 
   The function that @Mark_Bailey provided is shown below for convenince. 

 

A = [10 -12 4 8; 6 -9 8 0; 2 3 11 -2; 1 1 9 3];

sort matrix = Function( { m }, { t, b=[], i=[] },
 For( j = 1, j <= N Cols( m ), j++,
 t = A[0,j];
 b ||= Sort Descending( t );
 i ||= Matrix( Reverse( As List( Rank Index( t ) ) ) );
 );
 Eval List( { b, i } );
);

{ B, I } = sort matrix( A );

Show( A, B, I );

I also took the liberty of adding a little more along the same lines, so as to make it more generalized than my limited need :

 

sort matrix = Function( { m,order }, { t, b=[], i=[] },
							If(order == "ascending",
								For( r = 1, r <= N Cols( m ), r++,
									 t = A[0,r];
									 b ||= Sort Ascending( t );
									 i ||= Matrix( As List( Rank Index( t ) ) );
								   );
								Eval List( { b, i } );
								, 
								// else
								If(order == "descending",
									For( r = 1, r <= N Cols( m ), r++,
										 t = A[0,r];
										 b ||= Sort Descending( t );
								a		 i ||= Matrix(Reverse(As List( Rank Index( t ) )) );
									   );
									Eval List( { b, i } );
								  );  
							  );
							 
					 );

 

1 Comment
Ryan_Gilmore
Community Manager
Status changed to: Archived
We are archiving this request. If this is still important please comment with additional details and we will reopen. Thank you!