Borrar filtros
Borrar filtros

count number of elements for each slice of a vector

19 visualizaciones (últimos 30 días)
LH
LH el 10 de Jul. de 2024 a las 15:28
Comentada: LH el 10 de Jul. de 2024 a las 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.

Respuesta aceptada

Aquatris
Aquatris el 10 de Jul. de 2024 a las 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>

Más respuestas (0)

Categorías

Más información sobre Files and Folders en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by