Search code examples
matlabmatrixminimumindices

Find 2 or more indices (row and column) of minimum element of a matrix


I have this MATLAB code:

clear all;
clc;
A=[1 0 3;4 3 0;5 10 3];
[minimumelement,index]=min(min(A))

I want find the minimum element of matrix and their indices (row and column). Manually, we know the minimum element are 0, with indices (1,2) and (2,3). Now with code above I can't produce the indices (1,2) and (2,3). Anyone know how to find 2 or more indices (row and column) of minimum element of a matrix?


Solution

  • You can use find():

    [rows, columns] = find(A == min(min(A)));
    

    Furthermore, you can also specify how many matches to find:

    Nmatches = 1;
    [rows, columns] = find(A == min(min(A)), Nmatches);
    

    For completeness, you can also specify which matches to find, as in the first or last N matches, by using the 'first' or 'last' flag.