Find at least 4 consecutive values less than 1 in an array

I have an array in which I need to find four or more consecutive values that are less than one. For example: M = [1 0 .3 .5 .2 .1 6 7 .3 .5 10 1 .8 .9 .7 .2 .1 .3]
The program should return the indexes 3-6 and 13-18.
How can I go about solving this?

回答 (6 件)

Roger Stafford
Roger Stafford 2014 年 1 月 29 日
編集済み: Roger Stafford 2014 年 1 月 29 日

1 投票

Here's another one-liner if you accept initial indices as an answer.
f = find(all(hankel(M(1:4),M(4:end))<1,2));
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 29 日
編集済み: Azzi Abdelmalek 2014 年 1 月 29 日

0 投票

Edit
M= [1 0 .3 .5 .2 .1 6 7 .3 .5 10 1 .8 .9 .7 .2 .1 .3]
if diff(size(M))<0
M=M'
end
a=M<1;
b=strrep(num2str(a),' ','');
[ii,jj]=regexp(b,'1+','start','end')
d=jj-ii>=4
out=[ii;jj];
out=out(:,d)

3 件のコメント

Neha
Neha 2014 年 1 月 29 日
I got an error when I tried this:
Error using strrep Input strings must have one row.
Error in accelfilter (line 636) b=strrep(num2str(a),' ','');
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 29 日
編集済み: Azzi Abdelmalek 2014 年 1 月 29 日
If you copy and paste the code, there is no error, unless you are using M that is not a 1xn array. What is the size of M
size(M)
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 29 日
If M is a column array, just add at the beginning of your code
M=M'

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

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 29 日
編集済み: Azzi Abdelmalek 2014 年 1 月 29 日

0 投票

Edit
M= [1 0 .3 .5 .2 .1 6 7 .3 .5 10 1 .8 .9 .7 .2 .1 .3];
if diff(size(M))<0
M=M'
end
a=[0 M<1 0];
ii=strfind(a,[0 1]);
jj=strfind(a,[1 0])-1;
d=jj-ii>=4;
out=[ii;jj];
out=out(:,d)

3 件のコメント

Neha
Neha 2014 年 1 月 29 日
suppose that the array M length could change? How can I make the code still work for a matrix of size (1,n)?
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 29 日
It doesn't matter if the length changes. Maybe you are asking if the size changes, from 1xn to nx1. In this case we can edit our code. Look at edited answesrs
Neha
Neha 2014 年 1 月 29 日
Okay ,yes that works. Thank you very much.

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

José-Luis
José-Luis 2014 年 1 月 29 日

0 投票

M = [1 0 .3 .5 .2 .1 6 7 .3 .5 10 1 .8 .9 .7 .2 .1 .3];
numVal = 4;
your_vals = conv(double(M<1),ones(1,numVal)/numVal,'valid') == 1;
start = find(diff([0 your_vals]) == 1)
finish = fliplr(numel(M) + 1 - find(diff([0 fliplr(your_vals)]) == 1))
Andrei Bobrov
Andrei Bobrov 2014 年 1 月 29 日

0 投票

t = M<1&M>0;
t1 = [true;diff(t(:))~=0];
idx=accumarray(cumsum(t1),(1:numel(M))',[],@(x){x});
ii = idx(t(t1));
out = ii(cellfun(@numel,ii)>=4);
Jos (10584)
Jos (10584) 2014 年 1 月 29 日
編集済み: Jos (10584) 2014 年 1 月 29 日

0 投票

Here is a relatively simple one-liner:
M = [1 0 .3 .5 .2 .1 6 7 .3 .5 10 1 .8 .9 .7 .2 .1 .3] % example data
[startIDX, endIDX] = regexp(char((M(:).'< 1)+'0'), '1111+')

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2014 年 1 月 29 日

コメント済み:

2014 年 1 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by