Question about "min" command.

조회 수: 4 (최근 30일)
Manne Plok
Manne Plok 2018년 9월 6일
답변: Greg 2018년 9월 6일
I have a code that generates the following 2D array:
0 300 7500 10800 76800 86700
36300 30000 10800 7500 7500 10800
300 0 4800 7500 67500 76800
I want to find the row number in which the minimum number in a column is. For example, for column 3, the lowest number is 4800 so the output would be '3'.
However, for column 4, there is a tie (row 2 and 3 both have the lowest number). If I use the min command to generate the rows, it does all of them for me but for column 4 (in the event of a tie), it automatically picks the first one (row 2).
Is there any way for me to keep using min function for this, but adding in some sort of if statement (IDK how I would do that), OR, is there another way for me to find the lowest number per column and give the row output -- a way which tells me there's a tie?

채택된 답변

Greg 2018년 9월 6일
One option is to re-compare the first output of min to your original matrix. Then, count how many elements match in each column:
data = [1,2,3,4,5;6,7,8,9,10;11,2,13,14,15];
[minval,minloc] = min(data,[],1);
counts = sum(data==minval,1);
This relies on the implicit expansion feature of R2016b and later. Otherwise, use bsxfun to build the equality comparison to sum.

추가 답변 (0개)


Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기




Community Treasure Hunt

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

Start Hunting!

Translated by