Effacer les filtres
Effacer les filtres

count number of elements for each slice of a vector

19 vues (au cours des 30 derniers jours)
LH
LH le 10 Juil 2024 à 15:28
Commenté : LH le 10 Juil 2024 à 15:57
Hi all,
I have the folloiwng vector:
A = [2 1;
3 1;
4 2;
5 2;
6 2;
7 1;
8 1 ;
7 1;
5 2;
6 2];
You can see that the second column is either 1 or 2. I would liek to count the slices that has the same number without repetition, meaning that I want to count the first two number that have number 1 in their second column seprately from the three down the vectro (each slice separatly). As a result I wouldl ike to have something like:
num = [2 3 3 2];
Any help would be appreicted.
Thanks.

Réponse acceptée

Aquatris
Aquatris le 10 Juil 2024 à 15:40
Here is one simple way:
A = [2 1;
3 1;
4 2;
5 2;
6 2;
7 1;
8 1;
7 1;
5 2;
6 2];
tmp = find(diff(A(:,2))~=0); % find indeces where 2nd column values change
num = [tmp(1) diff(tmp') size(A,1)-tmp(end)] % desired output
num = 1x4
2 3 3 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 commentaire
LH
LH le 10 Juil 2024 à 15:57
Many thanks! Exactly what I need.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Files and Folders dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by