How do I pick a minimum that is not the first value in a matrix with multiple minima?
이전 댓글 표시
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
Azzi Abdelmalek
2014년 2월 5일
Can you give a short example? and post the expected result
Image Analyst
2014년 2월 6일
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
2014년 2월 6일
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
2014년 2월 6일
편집: Oladunjoye
2014년 2월 6일
Walter Roberson
2014년 2월 6일
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
2014년 2월 7일
답변 (1개)
Sajid Khan
2014년 2월 6일
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.
카테고리
도움말 센터 및 File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!