How do I select values bigger than a certain value & at least 4?

Hi there,
I need to find a way to make a matlab command for the following: I have an array with 2500x1 doubles. And I need to find the doubles that are at least 150 and when there are more than 4 values >150 next to each other.
For example [125 165 150 124 126 154 103 99 160 198 *140] should return [0 0 0 0 0 0 0 0 0 0 0] and [149 *156 158 159 168 *149 120 *150 160 140 125] should return [0 1 1 1 1 0 0 0 0 0 0]
I have the following: x = array > 150 & ????
I don't know how to do the at least 4 following values in a simple way, I can only think of an if loop...
Does anyone know if there is a command for that?
Thanks in advance!

 Akzeptierte Antwort

locations = strfind([0, YourArray > 150], [0 1 1 1 1]);
x = zeros(size(YourArray));
x([locations locations+1 locations+2 locations+3]) = 1;
Note: I made the code match your sample output, which does not agree with the written requirement. Your sample output has a location where exactly 4 in a row are > 150, but your written requirement was for more than 4 in a row are > 150.

4 Kommentare

Nice clean solution. I always forget about applying strfind to numeric arrays.
Thanks! And I see that I've made a mistake there.
Though now that I think of it, the above will have problems if there are more than 4.
mask = YourArray > 150;
starts = strfind([0, mask], [0 1 1 1 1]);
stops = strfind([mask, 0], [1 1 1 1 0]) + 3;
x = zeros(size(YourArray));
for K = 1 : length(starts)
x(starts(K) : stops(K)) = 1;
end
Atch, I was closer before ;-)
mask = YourArray > 150;
idx = find(mask(1:end-3) & mask(2:end-2) & mask(3:end-1) & mask(4:end));
x = zeros(size(YourArray));
x([idx,idx+1,idx+2,idx+3]) = 1;
This will handle the case of more than 4 without requiring any loops.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

First, download RunLength from the File Exchange. Then do this ...
VALUE_THRESHOLD = 150;
RUN_THRESHOLD = 4;
x = [149 156 158 159 168 149 120 150 160 140 125];
above = (x >= VALUE_THRESHOLD);
[b,n,bi] = RunLength(above);
meetsCriteria = b & n >= RUN_THRESHOLD;
start = bi(meetsCriteria);
run = n(meetsCriteria);
out = zeros(size(x));
for nr = 1:numel(run)
out(start(nr):start(nr)+run(nr)-1) = 1;
end

1 Kommentar

the cyclist
the cyclist am 9 Jan. 2017
Bearbeitet: the cyclist am 9 Jan. 2017
Note: It was not clear to me how to handle value exactly equal to 150, so you may need to adjust accordingly.

Melden Sie sich an, um zu kommentieren.

Let A be your 2500 by 1 row vector.
f = find(diff([false;A>150;false]));
f1 = f(1:2:end-1);
ix = f1(find(f(2:2:end)-f1>4));
The vector ix will have indices pointing to the first elements of A for series of more than four consecutive elements greater than 150.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by