Is there faster ranking for matrices than double sort()?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Marin Genov
il 16 Set 2022
Modificato: Marin Genov
il 16 Set 2022
I have a uint16 matrix, where I want to determine the rank of each element with respect to its column in sorted form. The sort function doesn't quite do this, but using the second output twice does it. I need to repeat this routine many times in my code, and profiling reveals that it's by far the slowest part. So, I was wondering if there is a faster way to accomplish this (double sort() seems a bit of an overkill for this perhaps).
Here is a MWE:
M = 50;
m = 50;
n = 2000;
A = randi(M,m,n,'uint16'); % generate an example matrix
% Ranking via double sort():
tic;
[~,I] = sort(A,1);
[~,R] = sort(I,1);
toc;
0 Commenti
Risposta accettata
Bruno Luong
il 16 Set 2022
Modificato: Bruno Luong
il 16 Set 2022
Not sure it is faster, but on the theorical complexity it is better (single sort).
A = randi(10,5,3)
[~,I] = sort(A,1);
[m,n] = size(A);
R = zeros(m,n);
R(I+(0:n-1)*m) = repmat((1:m)',1,n)
9 Commenti
Bruno Luong
il 16 Set 2022
Are you sure? it looks fine to me for toy example
A = randi(10,5,3);
[~,I] = sort(A,1);
[m,n] = size(A);
% double sort
[~,R] = sort(I,1)
% accumarray
R=accumarray(reshape(I+(0:n-1)*m,[],1),repmat((1:m)',n,1),[m*n,1]); % EDIT BUG FIX
R=reshape(R,m,n)
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrices and Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!