How to rank a vector with repeats without MATLAB unique?
Afficher commentaires plus anciens
Hi everybody,
I need to rank a vector and currently i'm using this two commands:
data_s=sort(data); [~,rank]=ismember(data,data_s);
For a vector data=[1 1 1 2 2 2 3 3 3] i get i=[1 1 1 4 4 4 7 7 7], but instead i would like to get i=[1 1 1 2 2 2 3 3 3], without using MATLAB function 'unique'. Anybody has any suggestion?
Thanks in advance.
3 commentaires
the cyclist
le 27 Juin 2016
Are you sure that that is what you want? If two runners tie for first place, and there is a third runner, then the ranking would typically be reported as
[1 1 3]
and not
[1 1 2]
So, the result you have now is typically what people would expect from a ranking system.
Guillaume
le 27 Juin 2016
"without using MATLAB function unique" Why?
It's possible to get the same output as unique using any of the set functions ismember, setdiff, union, etc., because internally they all call unique. But in that case, why not call unique directly?
If the reason is because it's an assignment, then using any of these functions just to avoid unique is probably not going to earn you a good mark. I would think that you'd be expected to come up with your own unique algorithm.
Réponses (2)
Jan Orwat
le 27 Juin 2016
Do you want to replicate following behaviour?
[~, ~, rank] = unique(data);
rank = rank.';
There are thousands of ways to do that. For example:
[foo, bar] = sort(data);
rank(bar) = cumsum([1, diff(foo)~=0]);
But, as the cyclist wrote, are you sure?
1 commentaire
pedrogon
le 28 Juin 2016
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!