Borrar filtros
Borrar filtros

How to find rank within group

7 visualizaciones (últimos 30 días)
Garima Sharma
Garima Sharma el 26 de Jun. de 2017
Comentada: Simon el 7 de Ag. de 2023
I have a dataset with 100 different groups, and between 8-11 entries in each group. I want to rank them, within a group, by the value of a variable y. How would I do this?
Concretely, if I have 2 groups with 2 people each such that: y=[1; 3; 6; 5] group_id=[1;1;2;2]
then I want a third vector: rank=[1;2;2;1]

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 27 de Jun. de 2017
Editada: Andrei Bobrov el 27 de Jun. de 2017
[~,a] = cellfun(@sort,accumarray(group_id,y,[],@(x){x}),'un',0);
rank = cell2mat(a);
or within for..end:
y=[1; 3; 6; 5];
group_id=[1;1;2;2];
rank = group_id;
a = unique(group_id);
for ii = 1:numel(a)
t = group_id == a(ii);
[~,rank(t)] = sort(y(t));
end
  2 comentarios
Garima Sharma
Garima Sharma el 27 de Jun. de 2017
Thanks, this works!
Simon
Simon el 7 de Ag. de 2023
The result created by the cellfun-based method is different from the for-loop based method when group_id is not in sorted order. The within-group ranking is the same in both methods. But rank = cell2mat(a) put all 1st group's within-group ranks together, followed by the second group's.
I have came across similar problem and in a subsequent analysis I need to put rank as a new column side-by-side next to group_id. The cellfun-based method would give me erroneous result.

Iniciar sesión para comentar.

Más respuestas (1)

Jess Lovering
Jess Lovering el 26 de Jun. de 2017
Do you mean that you want to sort the data by a specific array? You can do this using the sort function. It will also give you the indices of the new order so you can use that to apply to the other arrays, example:
y=[1; 3; 6; 5]
group_id=[1;1;2;2]
rank=[1;2;2;1]
[sorted_y,I]= sort(y)
sorted_group_id = group_id(I)
sorted_rank = rank(I)
  1 comentario
Garima Sharma
Garima Sharma el 27 de Jun. de 2017
no, what I want to do is rank y=[1; 3; 6; 5] within its respective group_id=[1;1;2;2].
So, y(1,1) is rank 1 in group 1; y(2,1) is rank 2 in group 1. y(3,1) is rank 2 in group 2, and y(4,1) is rank 1 in group 2. I want to only input vectors y and group, and get vector rank as the output.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by