please help me in sort row

1 visualizzazione (ultimi 30 giorni)
Triveni
Triveni il 8 Ago 2016
Commentato: Triveni il 8 Ago 2016
I have a row,
x = [0 0 0 0 0 40 40 -40 -40 -40 -40 -40 -40 80 80 80 80 80 80]; x1=x;
[xi, u_x] = hist(x1, unique(x1));
output,
u_x =
-40 0 40 80
xi
xi =
6 5 2 6
but i want to sort this according to given x,
[0 40 -40 80]
and
[5 2 6 6]
should be output of both x_u and x_i respectively. please help me.

Risposta accettata

Stephen23
Stephen23 il 8 Ago 2016
Modificato: Stephen23 il 8 Ago 2016
x = [0,0,0,0,0,40,40,-40,-40,-40,-40,-40,-40,80,80,80,80,80,80];
[uni,idu] = unique(x);
[cnt,idx] = hist(x,uni);
[~,ids] = sort(idu);
cnt = cnt(ids)
idx = idx(ids)
and the outputs:
cnt =
5 2 6 6
idx =
0 40 -40 80

Più risposte (0)

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by