How do I pick a minimum that is not the first value in a matrix with multiple minima?
Show older comments
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
Azzi Abdelmalek
on 5 Feb 2014
Can you give a short example? and post the expected result
Image Analyst
on 6 Feb 2014
Is M a cell array or an array of integers or doubles? You seem to be mixing them up. Give some code that will create a sample M for us. Make it easy for us to help you, not hard.
Walter Roberson
on 6 Feb 2014
What if the condition is not met? For example in one x3 = 7, x5 = 2, and in another x3 = 4, x5 = 6. Then the second one has the highest x5 but the first one has the highest x3 and no combination has the highest of both.
Oladunjoye
on 6 Feb 2014
Edited: Oladunjoye
on 6 Feb 2014
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
on 7 Feb 2014
Answers (1)
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
on 6 Feb 2014
Categories
Find more on Graph and Network Algorithms 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!