BookmarkSubscribe
Choose Language Hide Translation Bar

Ranking Tie with multiple duplicates

All,
This is a follow up question to https://community.jmp.com/t5/Discussions/Sort-a-matrix/m-p/85678#M38246

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

So, the current implementation of this function arbitrarily resolves ties. What I would like to do is deal with ties by preserving the order of the elements. For e.g.

A = [1 0 1];
{b,i} = sort_matrix(A);
b=

0     1     1

i=

2     1     3

I was hoping that using the Ranking Tie()  in place of Rank Index above and then reassigning the positions would help resolve it for me. However, in the case of more than 2 ties, the Ranking Tie() opens up another issue i.e., it provides a different result depending on whether the input has an even number of suplicates or odd number of duplicates . Any thoughts ?

Best
Uday
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Ranking Tie with multiple duplicates

Thank you for offering the solution. Adam helped me via the JMP Technical Support platform and here is the answer for anyone who is curious .

Clear Log(); Clear Globals();

/*A = [10 -12 4 8; 6 -9 8 0; 2 3 11 -2; 1 1 9 3];*/
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 );
i ||= Matrix(As List( Rank Index( -t ) )) ;
);
Eval List( { b, i } );
);
);

);
Best
Uday

Re: Ranking Tie with multiple duplicates

Thank you for offering the solution. Adam helped me via the JMP Technical Support platform and here is the answer for anyone who is curious .

Clear Log(); Clear Globals();

/*A = [10 -12 4 8; 6 -9 8 0; 2 3 11 -2; 1 1 9 3];*/
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 );
i ||= Matrix(As List( Rank Index( -t ) )) ;
);
Eval List( { b, i } );
);
);

);
Best
Uday