Effacer les filtres
Effacer les filtres

how to find the number of occurances

3 vues (au cours des 30 derniers jours)
johnson saldanha
johnson saldanha le 14 Déc 2018
Modifié(e) : madhan ravi le 14 Déc 2018
i have a matrix x=[1 1 1 1 2 2 1 1 3 3 ]
i want the output to be x1=[1 2 1 3] and y1=[4 2 2 2]
x1 should have the reduced number from input and x2 should be the number of ocurances
  1 commentaire
per isakson
per isakson le 14 Déc 2018
Search the File Exchange for run length encoding

Connectez-vous pour commenter.

Réponse acceptée

madhan ravi
madhan ravi le 14 Déc 2018
Modifié(e) : madhan ravi le 14 Déc 2018
x=[1 1 1 1 2 2 1 1 3 3];
idx=logical([diff(x)~=0,1]);
x1=x(idx)
yy=find(idx);
yyy=diff(find(idx));
y1=[yy(1) yyy]
x2=histc(x,unique(x)) % number of occurences
unique_numbers_and_their_occurences=[unique(x); histc(x,unique(x))].' %bonus
Gives:
x1 =
1 2 1 3
y1 =
4 2 2 2
x2 =
6 2 2
unique_numbers_and_their_occurences =
1 6
2 2
3 2
  4 commentaires
johnson saldanha
johnson saldanha le 14 Déc 2018
why are the number of columns of x1 and y1 not the same
madhan ravi
madhan ravi le 14 Déc 2018
Modifié(e) : madhan ravi le 14 Déc 2018
Try this I corrected the mistake:
x=[1;1;1;1;2;2;1;1;3;3];
idx=logical([diff(x)~=0;1]);
x1=x(idx)
yy=find(idx);
yyy=diff(find(idx));
y1=[yy(1);yyy]
x2=histc(x,unique(x)) % number of occurences
unique_numbers_and_their_occurences=[unique(x) histc(x,unique(x))]
Gives :
>> COMMUNITY
x1 =
1
2
1
3
y1 =
4
2
2
2
x2 =
6
2
2
unique_numbers_and_their_occurences =
1 6
2 2
3 2
>>

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by