フィルターのクリア

The number of consecutive ocurrances in an array

2 ビュー (過去 30 日間)
reham elnabawy
reham elnabawy 2017 年 4 月 30 日
コメント済み: Stephen23 2017 年 5 月 1 日
I have an array that is shown as: a=[0,0,1,1,1,0,0,1] and I would like to get the number of consecutive ocurrances in it along with the number itself meaning that I would like the output to be b=[2,0,3,1,2,0,1,1] where 0 occurred two consecutive times, 1 occurred three consecutive times, etc. Please help me and thank you in advance.
  3 件のコメント
John Chilleri
John Chilleri 2017 年 5 月 1 日
The b is a description of vector a:
Two 0s, then three 1s, then two 0s, then one 1.
Stephen23
Stephen23 2017 年 5 月 1 日
@Image Analyst: run length encoding.

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

回答 (1 件)

Guillaume
Guillaume 2017 年 5 月 1 日
This is trivially achieved with diff and find:
a = [0, 0, 1, 1, 1, 0, 0, 1];
transitions = find(diff(a));
runlengths = diff([0, transitions, numel(a)]);
runvalues = a([transitions, end]);
valuelength = reshape([runlengths; runvalues], 1, [])
  1 件のコメント
Stephen23
Stephen23 2017 年 5 月 1 日
+1 Nice and simple. That is the way to do it.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by