work with proportionality with vectors

4 views (last 30 days)
I have a vector
x=[1.05 2.023 3.1 4.014 12.112]
and another vector
y=[4.018 8.1 24.45]
how to detect that the values that follow the same proportionality with y are x(2) x(4) x(5) (proportionality with a tolerance)
I have been working in this for a long time and I do not get solution thanks

Accepted Answer

Walter Roberson
Walter Roberson on 4 Mar 2017
If you want the tolerance to be absolute, then define
abs_tolerance = 0.1; %for example
matchfun = @(P) ismembertol(x.*P, y, 1, 'DataScale', abs_tolerance);
piff = @(P)-sum(matchfun(P));
If you want the tolerance to be relative, then define
rel_tolerance = 1e-2; %for example
matchfun = @(P) ismembertol(x.*P, y, rel_tolerance);
piff = @(P)-sum(matchfun(P));
Now you can minimize piff over a range of values to find the constant of proportionality that gives you the most matches to within the tolerance. How exactly you do that is left as an exercise to the reader ;-) But with that data, if you allow an absolute tolerance of 0.1, then the best match is P in the range [2.0115035592480548, 2.0267363824759945] which gives 3 matches.
With the optimal P in hand,
selected_x = x(matchfun(P));
As for how to minimize: I suggest
P_range = linspace(LowerBound, UpperBound,5000);
results = arrayfun(piff, P_range);
acceptable_P = P_range(results == min(results));
and now select one of the acceptable_P as your representative P
  2 Comments
Luca cadalo
Luca cadalo on 4 Mar 2017
I am trying to use this function x0 = [-1 -4]; P = fminsearch(piff,x0) in order to guess with value minimize and I get this error
Matrix dimensions must agree.
i think it is because in ismembertol P is a number no a array of numbers
:(
Walter Roberson
Walter Roberson on 4 Mar 2017
fminsearch has to be provided with a single guess per variable, not a range. If the range is 0 to 4 use those as the lower and upper bound with the linspace and arrayfun I show.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!