フィルターのクリア

list the result of the index values in ranges

2 ビュー (過去 30 日間)
Nasser Ramsis
Nasser Ramsis 2017 年 10 月 22 日
コメント済み: Nasser Ramsis 2017 年 10 月 23 日
Hello,
I have an array that is populated with the values 1,2 or 3.
a = [1 1 2 2 3 3 2 2 1 1];
and I want to list the ranges of each value. The find function retrieves the index to each number which is great.
[col1] = find(a==1)
[col2] = find(a==2)
[col3] = find(a==3)
but is there a function or some logic i can use to list col1, col2, and col3 as ranges? as in col1 = 1-2 and 9-10 and col2 = 3-4 and 7-8 etc etc.
I appreciate any ideas or help.
Thank you in advance!
  1 件のコメント
Jos (10584)
Jos (10584) 2017 年 10 月 22 日
You might want to search for run length encoding.

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

採用された回答

Jos (10584)
Jos (10584) 2017 年 10 月 22 日
A = [1 1 2 2 3 3 2 2 1 1];
X = arrayfun(@(k) find(A==k),1:3,'un',0) ;
Y = cellfun(@(v) [v([true diff(v) > 1]) ; v([diff(v) > 1 true])]', X,'un',0) ;
% Y{k} contains the start indices (1st column) and end indices (2nd column) for a sequence of the value k in A
  1 件のコメント
Nasser Ramsis
Nasser Ramsis 2017 年 10 月 23 日
Thank you.
This is working great in 1 of my functions but this other function is giving me this message:
Index exceeds matrix dimensions.
Error in @(v)[v([true,diff(v)>1]);v([diff(v)>1,true])]'
Error in test (line 60) Y = cellfun(@(v) [v([true diff(v) > 1]) ; v([diff(v) > 1 true])]', X,'un',0) ;
Any ideas? do you want to see the entire code?

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 10 月 22 日
編集済み: Andrei Bobrov 2017 年 10 月 22 日
a = [1 1 2 2 3 3 3 2 2 1 1];
l = diff([0,a(:)'])~=0;
b = [strfind(l,[1 0]);strfind([l,1],[0 1])]';
out = accumarray(a(l)',1:size(b,1),[],@(x){sortrows(b(x,:))});

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by