17

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
smci
  • 32,567
  • 20
  • 113
  • 146
AG1
  • 6,648
  • 8
  • 40
  • 57
  • 1
    You must know that Octave and MATLAB are very similar. This is why I would suggest looking in the [corresponding MATLAB documentation](https://www.mathworks.com/help/matlab/ref/min.html) when having a difficulty. – Dev-iL Aug 10 '17 at 10:36
  • 3
    Almost every question asked here could be found in some doc somewhere. That's a poor reason to vote down a useful question. – Chris Sharp Nov 20 '17 at 01:54
  • FYI The octave doc `help min` says "If called with one input and two output arguments, `min` also returns the first index of the minimum value(s). Example `[x, ix] = min ([1, 3, 0, 2, 0])`" – smci Feb 21 '18 at 15:08

4 Answers4

19

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
AG1
  • 6,648
  • 8
  • 40
  • 57
  • 6
    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 – Ash Aug 10 '17 at 09:51
  • 1
    Not to mention `doc min`. A Google search on `octave min` also took me right to the link you posted. – beaker Aug 10 '17 at 16:33
  • IMHO, the documentation for Octave needs more examples for larger matrices with >1 dimension, the example in the documentation is for one row vector. – AG1 Aug 11 '17 at 02:29
8

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))
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)
Mac L. Lak
  • 121
  • 8
0

Given a matrix A of size m x n, you need to find the row number for the smallest value in the x column.


e.g A is 64x3 sized;

search_column = 3; 

[val,row] = min(results(:,search_column),[],1);

#row has the row number for the smallest value in the 3rd column.

Get the values for the first and second columns for the smallest row value

column1_value = A(row,1);
column2_value = A(row,2);
myeongkil kim
  • 2,465
  • 4
  • 16
  • 22