Turn on suggestions

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

Showing results for

- JMP User Community
- :
- Discussions
- :
- Discussions
- :
- Ranking Tie with multiple duplicates

Topic Options

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

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

Dec 14, 2018 12:14 PM
(632 views)

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

Uday

1 ACCEPTED SOLUTION

Accepted Solutions

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

@adammorris,

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

Uday

1 REPLY 1

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

@adammorris,

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

Uday