Octave: find the minimum value in a row, and also it's index

16,224

Solution 1

This is hard to find in the documentation. https://www.gnu.org/software/octave/doc/v4.0.3/Utility-Functions.html

octave:2> [minval, idx] = min(a, [], 2)
minval =

   1
   7
   4

idx =

   1
   3
   2

Solution 2

If A is your matrix, do:

[colMin, row] = min(A);
[rowMin, col] = min(A');

colMin will be the minimum values in each row, and col the column indexes. rowMin will be the minimum values in each column, and row the row indexes.

To find the index of the smallest element:

[colMin, colIndex] = min(min(A)); 
[minValue, rowIndex] = min(A(:,colIndex))

Solution 3

Suppose X is a matrix
row, col = Row and Column index of minimum value

[min_value, column_index] = min(X(:))
[row, col] = ind2sub(size(X),column_index)
Share:
16,224
AG1
Author by

AG1

Coding.

Updated on June 05, 2022

Comments

  • AG1
    AG1 almost 2 years

    How would one find the minimum value in each row, and also the index of the minimum value?

    octave:1> a = [1 2 3; 9 8 7; 5 4 6]
    a =
    
       1   2   3
       9   8   7
       5   4   6
    
  • Ash
    Ash over 6 years
    hum.... If you type >help min in octave... You see Built-in Function: min (X, [], DIM) and also a long, detailed explanation that explains what you want. So, not hard to find :p
  • beaker
    beaker over 6 years
    Not to mention doc min. A Google search on octave min also took me right to the link you posted.
  • AG1
    AG1 over 6 years
    IMHO, the documentation for Octave needs more examples for larger matrices with >1 dimension, the example in the documentation is for one row vector.