Finding index of minimum of a subset of an array

조회 수: 2 (최근 30일)
ZigzS
ZigzS 2018년 5월 1일
편집: Matt J 2018년 5월 1일
I want to find the index of the greater array for a subset within that array.
Example:
A=[2 3; 1 8; 2 6; 3 1;]
[row, col]=find(min(A([2 4], 2)));
This will compare the numbers 8 and 1, and I want to retrieve the index of A for the value of 1. Instead what this will output now is 2, since the subset being considered is [8 1]. What I want is for [row col] to equal [4 4], because this is the index in A where the minimum of the subset is located.
Any suggestions? Thanks,
  댓글 수: 2
Alfonso
Alfonso 2018년 5월 1일
I'm not sure if I have understood it correctly, you mean finding the minimum value for each row?
Matt J
Matt J 2018년 5월 1일
What I want is for [row col] to equal [4 4], because this is the index in A where the minimum of the subset is located.
I think you mean [4,2].

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

채택된 답변

Matt J
Matt J 2018년 5월 1일
편집: Matt J 2018년 5월 1일
B=A;
subset=false(size(A));
subset([2,4],2)=true;
B(~subset)=inf;
[row,col] = find(B==min(B(:))),

추가 답변 (1개)

Matt J
Matt J 2018년 5월 1일
편집: Matt J 2018년 5월 1일
Another way. This method is probably more efficient if you need to do this repeatedly with a large array A, but small subsets.
subset={[2,4],2}; %example
lookup=reshape(1:numel(A),size(A));
T=lookup(subset{:});
[minval,loc]=min( A(subset{:}) );
[row,col]=ind2sub(size(A), T(loc) )

카테고리

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