Re: Best way to get indices and values at sorting (rows/columns of) a matrix?

1,000 views
Skip to first unread message
Message has been deleted

Hakuna M.

unread,
May 9, 2016, 9:03:10 AM5/9/16
to julia-users
Hi there, I'm new to Julia but have some experience with Matlab.

To sort each column of a matrix M ( r rows, c columns) I can write in Matlab:

sry, there is an error with posting, I cant post the full text:

[v i] = sort (M)
return values are the sorted matrix v and the rowindex i of each value.
Example:
m =  3     2
     
1     4
     
6     5

[v i] = sort(m)

v
=  1     2
     
3     4
     
6     5

i
=  2     1
     
1     2
     
3     3

What is the best way to get the same results (sorted matrix, and indices) in Julia?

Message has been deleted
Message has been deleted

Hakuna M.

unread,
May 9, 2016, 9:13:09 AM5/9/16
to julia-users
currently I'm using:
i = mapslices(sortperm, m, 1);
offset
=repmat( (0:(c-1))'.*r ,r, 1); #'
v
= m[offset+i];

Hakuna M.

unread,
May 9, 2016, 9:13:48 AM5/9/16
to julia-users
but I don't like the storage allocation of 'offset'. Is there a better way to do it?
I would like to avoid for-loops.

Is there a way to access the current index of mapslices (ciom) ? And do something like this:

mapslices(x - > m[ x[:, ciom ], ciom], i, 1);

or do i need to do a for?

for ciom=1:c v[:,ciom] =m [ i[:, ciom ], ciom] ; end


other ideas?

Steven G. Johnson

unread,
May 9, 2016, 9:15:13 AM5/9/16
to julia-users

Steven G. Johnson

unread,
May 9, 2016, 9:26:48 AM5/9/16
to julia-users

Hakuna M.

unread,
May 9, 2016, 9:53:29 AM5/9/16
to julia-users


Am Montag, 9. Mai 2016 15:26:48 UTC+2 schrieb Steven G. Johnson:
Thanks for your answer. Sry, about my strange postings, there was an error, you have seen the other two postings below?.

sortperm(A, dim)

would be nearly the same as:
mapslices(sortperm, A, dim);
or?
But then I still need the values of the sorted matrix.
How can I get these? (maybe
sortperm!)


Steven G. Johnson

unread,
May 9, 2016, 1:03:18 PM5/9/16
to julia-users


On Monday, May 9, 2016 at 9:53:29 AM UTC-4, Hakuna M. wrote:
would be nearly the same as:
mapslices(sortperm, A, dim);
or?


No, I think sortperm(A, dim) would need to return an array of single-index indices (i.e. to be used with A[i]).  I posted a sample implementation for 2d arrays in the issue linked above, which should suffice for your use.


Hakuna M.

unread,
May 9, 2016, 2:03:34 PM5/9/16
to julia-users
Am Montag, 9. Mai 2016 19:03:18 UTC+2 schrieb Steven G. Johnson:


No, I think sortperm(A, dim) would need to return an array of single-index indices (i.e. to be used with A[i]).  I posted a sample implementation for 2d arrays in the issue linked above, which should suffice for your use.

Ah, ok that could be usefull as well. For current task I need the indices without offset for later usage. It could depend on the number of output parameters like Matlab.
Reply all
Reply to author
Forward
0 new messages