count number of elements for each slice of a vector

19 次查看(过去 30 天)
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.

采纳的回答

Aquatris
Aquatris 2024-7-10,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>

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Files and Folders 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by