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 Comments

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

Sign in to comment.

 Accepted Answer

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

More Answers (0)

Products

Release

R2019b

Asked:

on 5 Nov 2020

Commented:

on 6 Nov 2020

Community Treasure Hunt

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

Start Hunting!