Question about "min" command.

3 vues (au cours des 30 derniers jours)
Manne Plok
Manne Plok le 6 Sep 2018
Réponse apportée : Greg le 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?

Réponse acceptée

Greg
Greg le 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.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by