count number of elements for each slice of a vector
1 回表示 (過去 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.
0 件のコメント
採用された回答
Aquatris
2024 年 7 月 10 日
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
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!