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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by