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

4

16

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
Winded answered 10/8, 2017 at 8:29 Comment(3)
You must know that Octave and MATLAB are very similar. This is why I would suggest looking in the corresponding MATLAB documentation when having a difficulty.Amphictyon
Almost every question asked here could be found in some doc somewhere. That's a poor reason to vote down a useful question.Synopsis
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])"Beutler
W
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
Winded answered 10/8, 2017 at 8:33 Comment(3)
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 :pMalamut
Not to mention doc min. A Google search on octave min also took me right to the link you posted.Humanize
IMHO, the documentation for Octave needs more examples for larger matrices with >1 dimension, the example in the documentation is for one row vector.Winded
C
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))
Congener answered 10/1, 2018 at 20:44 Comment(0)
P
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)
Pennyworth answered 13/8, 2017 at 18:20 Comment(0)
L
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);
Laddy answered 2/1, 2021 at 1:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.