Question about "min" command.

3 次查看(过去 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
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 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by