How to select data outside a defined range

Hello All,
For a given data file, I'm interested to find the range of values that lie outside a range of values
Eg: For a range of values between -100 and +100, I only want the values that are greater than 20 and less than -20 (That means all the data values in between (i.e. between -20 and +20 are to be removed).
A = [-21 -15 -25 -10 0.4 10.6 17 24 44]
After condition check,
AOut = [-21 -25 24 44]
How can we carry out the same operation?
For this, I have implemented the below logic. It seems to work but I feel that it can be better optimised to be more efficient.
Cheers!
%TIn = Data file
[ii,jj] = find(~TIn);
Thr = zeros(size(TIn));
%Extract values that are below -15 and above +15
for k1=1:numel(TIn)
if (TIn(k1) < -15.0000)
Thr(k1) = TIn(k1);
elseif (TIn(k1) > 15.0000)
Thr(k1) = TVOut(k1);
end
end
%Remove zeroes
ThrO = nonzeros(Thr);

댓글 수: 2

Adam Danz
Adam Danz 2020년 11월 5일
편집: Adam Danz 2020년 11월 5일
Hint:
x(x<a & x>b)
% or
x(x<a | x>b)
Hello Adam,
Thanks for the suggestion.
I tried it out now with the below logic but am still not getting the correct output.
A = [-10 -15.4 12 0 16 20];
B = zeros(size(A));
for k1=1:numel(A)
if A(A(k1) < -15 & A(k1) > 15)
B(k1) = A(k1);
end
end

댓글을 달려면 로그인하십시오.

 채택된 답변

Adam Danz
Adam Danz 2020년 11월 5일
편집: Adam Danz 2020년 11월 5일
You were close.
You don't need a loop and you should use OR.
A = [-10 -15.4 12 0 16 20];
B = zeros(size(A));
idx = A<-15 | A>15;
B(idx) = A(idx)
B = 1×6
0 -15.4000 0 0 16.0000 20.0000
or
B(abs(A)>15) = A(abs(A)>15)
B = 1×6
0 -15.4000 0 0 16.0000 20.0000
or
B = A .* (abs(A)>15)
B = 1×6
0 -15.4000 0 0 16.0000 20.0000

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Descriptive Statistics and Visualization에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2020년 11월 5일

댓글:

2020년 11월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by