MATLAB: Work with proportionality with vectors

proportionality vector

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

Best Answer

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
Related Question