How can i calculate the largest interval when the elements of an array are larger than a value?
2 views (last 30 days)
Show older comments
Hi everyone!
i have a small questions. I have a large array whith elements with values that vary between 0 and 20. I want to determine the largest interval where the values are higher than 15. For example, if the array wass [ 0 6 5 2 16 17 16 11 12 14 5 6 9 16 17 18 18 9] then the anwer is four (16 17 18 18). Any good ideas?
0 Comments
Answers (6)
Roger Stafford
on 16 Sep 2013
Edited: Andrei Bobrov
on 16 Sep 2013
Letting A be your row array:
f = find(diff([false,A>15,false])~=0);
m = max(f(2:2:end)-f(1:2:end-1));
m is the length of the longest consecutive sequence of elements greater than 15. If you want to see that sequence, do this instead:
f = find(diff([false,A>15,false])~=0);
[m,p] = max(f(2:2:end)-f(1:2:end-1));
s = A(f(2*p-1):f(2*p)-1);
s will be the longest sequence.
0 Comments
Image Analyst
on 16 Sep 2013
If you have the Image Processing Toolbox you can simply do this:
m = [ 0 6 5 2 16 17 16 11 12 14 5 6 9 16 17 18 18 9]
m15 = m > 15
% Label the blobs
labeled = bwlabel(m15);
% Measure the lengths of stretches where it's > 15
measurements = regionprops(labeled, 'Area');
allAreas = [measurements.Area]
[sortedLengths, labels] = sort([measurements.Area], 'Descend')
largestLabelIndex = labels(1)
% Keep that one
keeper = ismember(labeled, labels(1))
% Extract the values
out = m(keeper)
0 Comments
Azzi Abdelmalek
on 16 Sep 2013
Edited: Azzi Abdelmalek
on 16 Sep 2013
a=[ 0 6 5 2 16 17 16 11 12 14 5 6 9 16 17 18 18 16 17 18 19 20 16 17 9];
b=a>15;
id= [0 diff(b) 0];
id1=find(id==1);
id2=find(id==-1)-1;
for k=1:numel(id1)
d=a(id1(k):id2(k));
c=[0 diff(d)>=0 0];
ii=find(c==0);
ii1=ii(1:end-1);
ii2=ii(2:end)-1;
[jj,jj]=max(ii2-ii1);
v{k}=d(ii1(jj):ii2(jj));
end
[idx,idx]=max(cellfun(@numel,v));
out=v{idx}
0 Comments
Kevin Claytor
on 16 Sep 2013
We can find the sequence using find and diff;
seq = [0 6 5 2 16 17 16 11 12 14 5 6 9 16 17 18 18 9]
thresh = 15
gt = find(seq>thresh)
df = diff([0, gt])
Now we have something that looks like a bunch of ones... we can use a previous solution on answers to get the length of that sequence.
% First put it into the format of ones and zeros
x = df; x(x>1) = 0
v = diff([0, x, 0]);
len = findstr(v, -1) - findstr(v, 1);
Now, if you just want the max, it's easy; max(len)+1 (we lost one in the first diff). If you want the corresponding index... that could be trickier. I'll leave that to you.
0 Comments
Andrei Bobrov
on 16 Sep 2013
Edited: Andrei Bobrov
on 16 Sep 2013
x = [ 0 6 5 2 16 17 16 11 12 14 5 6 9 16 17 18 18 9];
xx = x(:) > 15;
t = cumsum([false;diff(xx) == 1]);
z = accumarray(t(xx),find(xx),[],@(x){x});
[~,ii] = max(cellfun('length',z));
out = x(z{ii});
0 Comments
Jan
on 16 Sep 2013
Edited: Jan
on 16 Sep 2013
x = [0 6 5 2 16 17 16 11 12 14 5 6 9 16 17 18 18 9];
[b, n, index] = RunLength(x > 15);
[maxN, maxPos] = max(n .* b);
Result = x(index(maxPos) + (0:maxN - 1));
Note: The current submission is incomplete, an updated one will appear tomorrow. RunLength_M is working and included in the submission also. There are many more run-length tools in the FEX, e.g. FEX: rude.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!