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

Viewed 17360

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
4 Answers

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))

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);
Related