# Find min in matrix given specific rows and columns?

62 views (last 30 days)
Andrew Poissant on 1 Aug 2017
Commented: Star Strider on 2 Aug 2017
I have a matrix, A, and I want to find the minimum value given specified row and column vectors, r and c. I also want to return the col and row of that minimum value in A. Any idea how to find the min? Given A, r, and c, the answer for this scenario should be a min value of 1 because it is in row 1, col 1, which is a part of the r and c vectors.
A = [1 12 23 19 1 13;
2 3 13 34 5 75;
5 22 45 5 1 94;
4 5 68 2 5 17;
2 4 34 11 13 92];
r = [1 3 4];
c = [1 2 3];

Star Strider on 1 Aug 2017
One approach:
idx = sub2ind(size(A), r', c'); % Define ‘Eligible’ Elements As Linear Index Vector
[minval,minidx] = min(A(:)); % Minimum & Location (Linear Index)
[rmin,cmin] = ind2sub(size(A),intersect(minidx,idx)); % Recover Row, Column Indices Of ‘Eligible’ Minimum
fprintf('\nMinimum of A = %d, at [%2d,%2d]\n', minval, rmin, cmin)
Minimum of A = 1, at [ 1, 1]

Show 1 older comment
Star Strider on 2 Aug 2017
You didn’t state the changes you made in the matrix to produce an empty result, so I can’t specifically address that problem.
My code works by finding the minimum of the ‘eligible’ elements, then finding their coordinates in the larger matrix, and returning the minimum and the indices of the minimum.
A more robust version (that I wrote later to account for multiple occurrences of the minimum among the ‘eligible’ elements) is:
idx = sub2ind(size(A), r', c'); % Define ‘Eligible’ Elements As Linear Index Vector
minval = min(A(idx)); % Minimum of ‘Eligible’ Values
minidx = find(A(:)==minval); % Location Of All Values Of ‘Eligible’ Minimum (Linear Index)
[~,minA] = ismember(idx, minidx); % Find All ‘Eligible’ Minima
[rmin,cmin] = ind2sub(size(A),intersect(minidx,idx)); % Recover Row, Column Indices Of All ‘Eligible’ Minimum
fprintf('\nMinimum of A = %d, at [%2d,%2d]\n', [minval*ones(size(rmin,1),1), rmin, cmin]')
I don’t know if it would solve the problem with your changed matrix, since I don’t know the changes you made to it.
Andrew Poissant on 2 Aug 2017
I changed the value of 1 in cell [1,1] to 11 and it gave the empty cell. However, your edit is working perfectly. Thanks for the help!
Star Strider on 2 Aug 2017
My pleasure!

### More Answers (1)

Ganesh Hegade on 1 Aug 2017
You can use this.
minRow = min(min(A(r, :)));
minCol = min(min(A(:, c)));

#### 1 Comment

Andrew Poissant on 2 Aug 2017
I am confused by this response. This finds the minimum for the row and column separately or am I not understanding what you are doing? I need something that will take the rows and columns specified in the r and c vectors, respectively, and search those defined rows and columns for the minimum value. How would I use this to obtain my answer? Thanks
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}