group equal value in array

3 ビュー (過去 30 日間)
Riccardo Minati
Riccardo Minati 2019 年 10 月 4 日
編集済み: the cyclist 2019 年 10 月 4 日
Hi,
I have one 1x2048 array with repeated values:
A = [0 0 0 255 255 255 0 0 0 0 0 255 255 255 255 255 0 0 0 0]
and I would like to group similar element so that the output would be this:
B = [0 255 0 255 0]
I need to retain the altenation of the values because at the end of this I need to count how many 255 and 0 are there.
Any help would be much apreciated
Thanks

回答 (1 件)

the cyclist
the cyclist 2019 年 10 月 4 日
編集済み: the cyclist 2019 年 10 月 4 日
If you get the RunLength utility from the File Exchange, it will give you the output vector you want and the counts, in one function call.
>> [B N] = RunLength(A)
B =
0 255 0 255 0
N =
3 3 5 5 4

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by