How to find first occurrences of a number in the beginning of blocks of that number?

14 ビュー (過去 30 日間)
Mr M.
Mr M. 2018 年 2 月 27 日
編集済み: Stephen23 2018 年 2 月 27 日
For example I have x = [1 2 2 3! 0 0 0 3! 3 3 0 1 1 3! 3 4]; And I need indices of first threes associated its block. Signed with ! In this case indices are: 4,8,14

回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 2 月 27 日
targetval = 3;
num_wanted = 3;
find(x==targetval, num_wanted, 'first')

Stephen23
Stephen23 2018 年 2 月 27 日
編集済み: Stephen23 2018 年 2 月 27 日
>> vec = [1,2,2,3,0,0,0,3,3,3,0,1,1,3,3,4];
>> idx = diff(vec==0)>0;
>> idb = find([true,idx]);
>> ide = find([idx,true]);
>> foo = @(v)v&cumsum(v)==1;
>> baz = @(b,e)foo(3==vec(b:e)); % set value to match here.
>> tmp = arrayfun(baz,idb,ide,'uni',0);
>> find([tmp{:}])
ans =
4 8 14

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by