필터 지우기
필터 지우기

find max value within set parameters of matrix

조회 수: 1 (최근 30일)
Daniel Hamilton
Daniel Hamilton 2018년 3월 12일
편집: Stephen23 2018년 3월 12일
say i have a matrix a = [ 0 1 2 3 4 5 6 7 8 9 10 11]
I know how to find the max of that matrix and its location but say I want to find the max number and its location of only cells a((1,2):(1:3), (3,2):(3,3)) how would i do this? I've tried just adapting : [row, col] = find(ismember(a, max(a(:)))) to [row, col] = find(ismember(a, max(a(in1:in2,in3:in4)))) where in1 represents the input(1,2) and so forth but it returns a whole load of values instead of just the one
  댓글 수: 2
Jos (10584)
Jos (10584) 2018년 3월 12일
I do not understand your indexing notation, as in a((1,2):(1:3), (3,2):(3,3))! What is, for instance (3,2) supposed to represent? And what about the colons in (1:3), and also between ():()?
Stephen23
Stephen23 2018년 3월 12일
What does a((1,2):(1:3), (3,2):(3,3)) mean? Please convert this to subscript indexing.

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

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 3월 12일
% Here code is, hope I undestand your question
array=[1 2 3; 9 7 0; 12 7 51]; % You can change the matrix size
max_value=array(1,1);
for i=1:3
for j=1:3
if max_value<=array(i,j);
%here I have used only two locations, you can expand it multiple
if (array(i,j)==array(1,2)) || (array(i,j)==array(1,3))
max_value=array(i,j);
end
end
end
end
  댓글 수: 1
Stephen23
Stephen23 2018년 3월 12일
Requires nested loops and multiple if's: see Jos' answer for a simpler, neater solution.

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

추가 답변 (1개)

Jos (10584)
Jos (10584) 2018년 3월 12일
If you want to mask out elements of a vector for finding the max, without disrupting the location within the vector, you might consider replacing these elements by -Inf
a = [1 2 3 4 5 4 3 2 1]
maskix = [2:6]
b = a % work on a copy
b(maskix) = -Inf
[mv, mi] = max(b)
  댓글 수: 1
Stephen23
Stephen23 2018년 3월 12일
편집: Stephen23 2018년 3월 12일
+1 good use of MATLAB. Using NaN would also allow min, mean, etc.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by