How do I select values bigger than a certain value & at least 4?
10 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Walter Roberson
2017-1-9
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 个评论
Walter Roberson
2017-1-10
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
Walter Roberson
2017-1-10
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.
更多回答(2 个)
the cyclist
2017-1-9
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 个评论
the cyclist
2017-1-9
编辑:the cyclist
2017-1-9
Note: It was not clear to me how to handle value exactly equal to 150, so you may need to adjust accordingly.
Roger Stafford
2017-1-9
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!