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
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.
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.
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 .
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.
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
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))
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With