2

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
2
  • can you please post an example of the both result vectors you are expecting in the given example? It's very hard to understand what you are trying to do. Even it's confusing how overproduction and underproduction is defined. Commented Oct 26, 2016 at 11:20
  • I have made in very simple language, Please see the modified question . Now I think you would understand what I mean ? Commented Oct 26, 2016 at 12:27

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

7 Comments

I guess this is the way to go if you want 'the closest match', rather than 'a close match'.
@Dennis you're right. As I understood from the question, one single result is requested. 'a close match' means several results are possible?
It is quite possible that you give the asker what he was looking for. My thought was mainly that a close match, might mean that sometimes 0 results could come back.
I have one more question how I can proceeded the same thing if 'In' and 'ref' size are different.For example if 'In' size of 20x1 while 'ref' size of 200x1 any suggestion ?
@AnkitaDebnath Both vectors have a different size in your example ('In': 5; 'Ref': 6). So for the code example it makes no difference.
|
0

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) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.