Ankita
Ankita

Reputation: 485

Filtering of data based on condition using matlab

I have ref value as

ref = [9.8 13 10.51 12.2 10.45 11.4]

and In values as

In = [10.7 11 11.5 11.9 12]

I want to do following two things :

  1. Identify which In value closest matches with ref value and then after
  2. To check whether the matched In value is lower or higher than ref value. If it is lower than saved in array1 and if it is higher than saved in array2

Upvotes: 2

Views: 114

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21561

If you have a fixed deviation that is allowed for values to be 'close', the key part of your question can be solved with the ismemberf File Exchange Submission.

Basic syntax:

[tf, loc]=ismemberf(0.3, 0:0.1:1) 

Can be exended by defining the allowed tolerance:

[tf, loc]=ismemberf(0.3, 0:0.1:1, 'tol', 1.5) 

Upvotes: 0

bushmills
bushmills

Reputation: 673

See the following code snippet as one of many solutions:

% it would be a much better style 
% to initialize the result vectors here properly!
a1 = [];
a2 = [];

for i=1:length(P_in)
    [value, ind] = min(abs(P_in(i) - P_ref));

    if P_in(i) <= P_ref(ind)
        a1 = [a1 P_in(i)];
    else
        a2 = [a2 P_in(i)];
    end;
end;

with the given vectors

P_ref = [9.8 13 10.51 12.2 10.45 11.4];
P_in = [10.5 11 11.5 11.9 12];

I get the following result:

array1 = [10.5000   11.0000   11.9000   12.0000]
array2 = [11.5000]

Upvotes: 1

Related Questions