Count Adjacent Equal Elements in a Vector

Hey guys, I'm very new to matlab. I have been struggling to write a code that counts adjacent equal numbers in a vector and then puts all values in matrix. For example, in a vector like x = [1 3 4 5 1 5 5 4 4 4], count adjacent and equal elements. The result should be like:
yMatrix = [1 3 4 5 1 5 4
1 1 1 1 1 2 3]

回答 (1 件)

Matt J
Matt J 2015 年 10 月 31 日
編集済み: Matt J 2015 年 10 月 31 日

0 投票

dx=diff([inf,x])~=0;
yMatrix = [x(logical(dx)) ; ...
accumarray(cumsum(dx(:)),1).'],

カテゴリ

ヘルプ センター および File ExchangeMatrix Computations についてさらに検索

質問済み:

2015 年 10 月 31 日

編集済み:

2015 年 10 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by