Count consecutive elements in an array

2 ビュー (過去 30 日間)
Debraj Midya
Debraj Midya 2021 年 10 月 6 日
コメント済み: Mathieu NOE 2021 年 10 月 7 日
suppose i have an array like: A = [1 1 1 1 2 2 2 4 4 4 4 1 1 1 1 2]
I want the result in this form, B = [4 3 4 4 1].
or something like this will be best: B = [4 1; 3 2; 4 4; 4 1; 1 2]. First colums contains the number of entry and the second colums contains the entry itself.
Kindly help.

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 10 月 6 日
hello
try this :
A = [1 1 1 1 2 2 2 4 4 4 4 1 1 1 1 2];
% A = [1 2 2 4 4 1 1 2];
%% main code
p=find(diff(A)~=0);
s1 = [p(1) diff(p) numel(A)-p(end)];
s2 = A(cumsum(s1));
B = [s1' s2'] % First colums contains the number of entry and the second colums contains the entry itself
  2 件のコメント
Debraj Midya
Debraj Midya 2021 年 10 月 6 日
This worked. Thank you Sir.
Mathieu NOE
Mathieu NOE 2021 年 10 月 7 日
my pleasure

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by