Question about "min" command.

10 views (last 30 days)
Manne Plok
Manne Plok on 6 Sep 2018
Answered: Greg on 6 Sep 2018
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?

Accepted Answer

Greg
Greg on 6 Sep 2018
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.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!