MATLAB: Suppose I have three vector x1,x2 and Y. Iam using optimization algorithm, I want maximum value of Y by using C=max(Y) I can extract the value and the value of x1 and x2 which are giving max Y value accordingly extracted, but I need the min value o

mathematicsMATLAB

My algorithm concentrate only on max Value of Y not minimum value of X=x1+x2.
x1= [ 2 5 6 7 8 3],
x2= [ 3 5 6 8 9 3],
X=x1+x2=[ 5 10 12 15 17 6],
Y= [30 22 40 40 40 40],
max value of Y is 40 so it pick up 12 not 6.

Best Answer

mxy=max(Y); % find max Y
iy=find(Y==mxy); % all locations == max
mxymnx=min(X(iy)); % find corresponding min X for those positions
NB: "==" works for integer values of X,Y as given here; if real data is floating point use ismembertol for comparisons as exact tests may fail on occasion depending on FP rounding and how values are calculated.
NB2: To qualify the above a little-- "==" will work for locating Y==mxy as the value returned will be the exact bit pattern of the maximum value found; the question would be whether there were other values approximating mxy that are close but not exactly the same that would be intended to have been found. This could happen if the population of the array were generated by slightly different routes such that one value came from reading an input file while another was computed internally; those two results could be intended to be the same value but not absolutely identical to the LSB.
Related Question