How do I pick a minimum that is not the first value in a matrix with multiple minima?

2 views (last 30 days)
Hi everyone.
I have a two column matrix M where the first column is a vertor/cell containing five elements x1, x2, x3, x4, x5 and the second column is a number. Many combinations of the x's in the first have the same correspoding number in the second column. I want to pick the minimum number in the second column with the corresponding cell in the first column that has the highest x3 and x5 out all available combinations. I used
[~, ind] = min([M{:,2}]);
P(j,:) = M(ind,:);
%Here I store the minimum and the corresponding cell in matrix P
but it picks the first minimum. I do not want just the minimum but the minimum highest x3 and x5. How do I do this?
  6 Comments
Walter Roberson
Walter Roberson on 6 Feb 2014
I still do not see any reason why there is always going to be a case in which both x3 and x5 are largest, no reason why it is not possible that in one case x3 is largest but x5 is not largest.
Oladunjoye
Oladunjoye on 7 Feb 2014
Dear Walter Robertson,
Thanks a lot.
My main the problem is that the code returns the row of the first minimum it comes across,
[0;0;0;0;0] -4,
in this case.
My desired result is that the minimum in the second column should have a corresponding vector in the first column with the x's having values x1= -x2, x3 = -x4 and x5 > 0 just as in the example above.
How do I do this?

Sign in to comment.

Answers (1)

Sajid Khan
Sajid Khan on 6 Feb 2014
2 6
[4,5] 3
[1,8,7] 8
[3,5,8,7] 2
[3,5,6,9,11] 9 if M is the cell posted above. the the step
[~, ind] = min([M{:,2}]);
will return ind = 4. Which is correct in my case. But I guess you have to replace your second step by the following one
P = M(ind,1);
Because the cell elements that corresponds to minimum second column element lies in the first column, so it isn't good to search it in whole array by typing
P = M(ind,:);
If you don't want the result to be in form of a cell, then use the following syntax to convert it into a matrix
N = cell2mat(P).
If you have further questions, then you are most welcome to ask.
  1 Comment
Oladunjoye
Oladunjoye on 6 Feb 2014
Dear Sajid Khan,
Thank you.
I have tried the code above but it did not give the desired result. I have given further details below.
M is obtained from an iteration and it's actually a 26604x2 matrix. Below is an example of what matrix M looks like for a single iteration say j=0.
The column is a vector and the second column is a number.
[0;0;0;0;0] -4
[0;0;1;1;0] -4
[0;2;0;1;1] -4
[0;0;0;0;1] -2
[1;2;1;3;1] -2
[0;0;2;-2;3] -4 (preferred)
[1;-1;0;0;3] -4
[1;1;1;0;2] -4
The one marked preferred is the minimum I want out of all the minimum value.
How do I do this?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!