How to calculate minimum if there are more maximum values in a column?

1 view (last 30 days)
Hello all, I am still new in MATLAB and I am a slow learner so, please your help will be appreciated.
I have a matrix 16*1, example A = [ 0.3907 0.3907 0.4650 0.2642 0.465 0.2642 0.465 0.3197 0.465 0.465 0.3197 0.465 0.3907 0.3907 0.465 0.465 ]
At the same time I have another matrix also 16*1
B = [ 0.8807 1.3866 1.5753 0.9959 1.6291 0.935 2.0934 1.1718 1.7681 2.0563 0.6348 1.4603 1.4598 1.2062 1.3946 2.0387 ] I made code as follows:
[~, idx] = max(A); Best_min = find(B == min(B([1:idx-1 idx+1:end])));
But it gives wrong answer because we have more than one maximum in matrix A. And I know that the solution done by me is perfect only when we have one maximum.
What should I do in order to find the minimum from matrix B but the rows having maximum value in A should be excluded. Thank you.

Accepted Answer

Star Strider
Star Strider on 18 Jan 2016
If I understand correctly what you want, this will work:
A = [ 0.3907 0.3907 0.4650 0.2642 0.465 0.2642 0.465 0.3197 0.465 0.465 0.3197 0.465 0.3907 0.3907 0.465 0.465 ];
B = [ 0.8807 1.3866 1.5753 0.9959 1.6291 0.935 2.0934 1.1718 1.7681 2.0563 0.6348 1.4603 1.4598 1.2062 1.3946 2.0387 ];
Amax = find(A == max(A));
Bmin = find(B == min(B));
Best_min = setdiff(Bmin,Amax); % Index Of Minimum Meeting Constraints
Best_min_mtx = [Best_min, B(Best_min)] % Index And Value For ‘B’
Best_min_mtx =
11 0.6348

More Answers (2)

Stephen23
Stephen23 on 18 Jan 2016
Edited: Stephen23 on 18 Jan 2016
You need to learn how to use logical indexing, which is a very fast and simple way to access data inside arrays:
>> X = [1,4,9,1];
>> idz = max(X)==X
idz =
0 0 1 0
We can then get the corresponding value from X simply by using the logical index:
>> X(idz) % its value
ans = 9
>> find(idz) % its subscript index
ans = 3
Note that testing for equivalence of floating point values should be avoided, as it is very unreliable. Because in your case you have to compare floating point numbers, I would recommend that you use a tolerance to make a reliable test, like this:
A = [0.3907,0.3907,0.4650,0.2642,0.465,0.2642,0.465,0.3197,0.465,0.465,0.3197,0.465,0.3907,0.3907,0.465,0.465];
B = [0.8807,1.3866,1.5753,0.9959,1.6291,0.935,2.0934,1.1718,1.7681,2.0563,0.6348,1.4603,1.4598,1.2062,1.3946,2.0387];
% tolerance for floating point equivalence:
tol = 0.00005;
% indices of maximum value in A:
idx = abs(max(A)-A)<tol;
% get minimum value:
min(B(~idx))
displays this:
ans = 0.63480
If you also need its location index in B, then try this:
% indices of minimum value in B, excl. idx:
idy(~idx) = abs(min(B(~idx))-B(~idx))<tol;
% minimum value:
B(idy)
% minimum location:
find(idy)
this gives the minimum value and location as:
ans = 0.63480
ans = 11

Image Analyst
Image Analyst on 18 Jan 2016
I don't know where those numbers came from, but in general you should be aware of the FAQ when comparing doubles with ==.
If you just hand-typed in the numbers, you will probably be okay with the two answers given so far.
  3 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!