If same value apperas in array count +1

1 回表示 (過去 30 日間)
First Last
First Last 2021 年 9 月 14 日
コメント済み: First Last 2021 年 9 月 14 日
Hello Guys,
I have an array with nx1, now i need a code to count + 1 if the same value apperas later in the array, for example:
input = [ 1 2 3 4 1 2 3 4 1 2 3 4 ];
output = [ 1 1 1 1 2 2 2 2 3 3 3 3];
Thank you

採用された回答

Walter Roberson
Walter Roberson 2021 年 9 月 14 日
input = [ 1 2 3 4 1 2 3 4 1 2 3 4 ];
output = sum(triu(input' == input))
output = 1×12
1 1 1 1 2 2 2 2 3 3 3 3
  1 件のコメント
First Last
First Last 2021 年 9 月 14 日
Thats it, thank you Walter :) you saved my day

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

その他の回答 (1 件)

KSSV
KSSV 2021 年 9 月 14 日
input = [ 1 2 3 4 1 2 3 4 1 2 3 4 ];
a = reshape(input,[],3)' ;
[c,ia,ib] = unique(a,'rows') ;
ib
  1 件のコメント
First Last
First Last 2021 年 9 月 14 日
Thank you KSSV,
but i the output is a little bit diffrent

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by