Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 878
AG1 Avatar asked Aug 10 '17 08:08

AG1


People also ask

How do you find the index of the minimum value in a vector in octave?

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.

How do you find the minimum of a matrix?

M = min( A ,[], dim ) returns the minimum element along dimension dim . For example, if A is a matrix, then min(A,[],2) is a column vector containing the minimum value of each row.

How do you find the minimum value in an array in Matlab?

C = min(A,B) returns an array the same size as A and B with the smallest elements taken from A or B . C = min(A,[],dim) returns the smallest elements along the dimension of A specified by scalar dim . For example, min(A,[],1) produces the minimum values along the first dimension (the rows) of A .

How do you access the matrix element in octave?

Accessing the elements of the matrix : The elements of a matrix can be accessed by passing the location of the element in parentheses. In Octave, the indexing starts from 1.


3 Answers

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
like image 59
AG1 Avatar answered Oct 22 '22 10:10

AG1


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))
like image 43
Odd Marius Aakervik Avatar answered Oct 22 '22 09:10

Odd Marius Aakervik


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)
like image 3
Mac L. Lak Avatar answered Oct 22 '22 09:10

Mac L. Lak