Search code examples
matlabminimumindices

Index row and index column of the minimum value matrix A in 2nd until 3rd row and 2nd column


I have MATLAB code as below:

clear all;
clc;
A=[1 0 3;4 3 0;5 10 3]
[rows,columns]=find(A(:,2)==min(A(2:3,2)))

I want to find index row and index column of the minimum value matrix A in 2nd until 3rd row and 2nd column. Manually, we can find the minimum value is 3 with index row=2 and index column=2 (see below image)

enter image description here

But my problem if I use code above, the result is not same. Anyone can help me?


Solution

  • Since you are inputting the column index so there is no need to find that again. The better approach would be to just do:

    col_ind = 2;
    [minimum, row_ind]  = min(A(2:3,col_ind)); 
    row_ind = row_ind+1;      %1 is added since first row is skipped in above line
    

    In your code,

    • A(2:3, 2) has only 1 column. So columns will always be 1.
    • If A(1,2) is also the same as the minimum value, your code will return the row index of that first value.
    • The fact that the first row and column are skipped in calculations is not incorporated in outputs.

    Fixing above problems:

    [rows,columns]=find(A(2:3,2)==min(A(2:3,2)));
    rows=rows+1;     columns=columns+1;