Find min in matrix given specific rows and columns?

조회 수: 27 (최근 30일)
Andrew Poissant
Andrew Poissant 2017년 8월 1일
댓글: Star Strider 2017년 8월 2일
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
Star Strider 2017년 8월 1일
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]
  댓글 수: 4
Andrew Poissant
Andrew Poissant 2017년 8월 2일
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
Star Strider 2017년 8월 2일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ganesh Hegade
Ganesh Hegade 2017년 8월 1일
You can use this.
minRow = min(min(A(r, :)));
minCol = min(min(A(:, c)));
  댓글 수: 1
Andrew Poissant
Andrew Poissant 2017년 8월 2일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by