Check for identical maximum values in an array

Hi guys, I have a matrix, A = [2 4 1;8 3 2;8 8 6;4 5 3] As seen, the above matrix has more than one maximum values. How do i write an if statement testing for multiple maximum values? That is: if (code) % if more than one maximum value exists; ...; % do ... end

 채택된 답변

Walter Roberson
Walter Roberson 2017년 9월 6일

1 개 추천

"if more than one maximum value exists in the array pick the first max value and display it's index"
idx = find( A(:) == max(A(:)) );
if length(idx) > 1
disp(idx(1))
end

추가 답변 (3개)

Star Strider
Star Strider 2017년 9월 6일

2 개 추천

Probably the easiest way is to do logical indexing and count the number of non-zero elements to get the number of maximum values:
A = [2 4 1;8 3 2;8 8 6;4 5 3];
nr_maxvals = nnz(A == max(A(:)));
nr_maxvals =
3

댓글 수: 3

Jane Ak
Jane Ak 2017년 9월 6일
thank you Star. I'm new to matlab, how can i apply this result to the "if statement"?
if nnz(A == max(A(:))) > 1
...
end
@Walter — Thank you.

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

José-Luis
José-Luis 2017년 9월 6일
편집: José-Luis 2017년 9월 6일

1 개 추천

Number of maximum values:
numMax=sum(A(:) == max(A(:)));
if numMax > 1
%Do your thing
end

댓글 수: 2

Jane Ak
Jane Ak 2017년 9월 6일
편집: Jane Ak 2017년 9월 6일
Thank you Jose. Pardon my asking, why is the sum there? Also my if statement is stated thus:
if %more than one maximum value exists in the array
%pick the first max value and display it's index
end
I'm thinking of how to apply this to your answer above. Thank you again for your time
@Jane Ak: Star Strider already explained that in their answer from three hours ago.

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

Jane Ak
Jane Ak 2017년 9월 6일

0 개 추천

Thanks alot guys. I thought I could accept more than 1 answer. you guys are the real MVPs.

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2017년 9월 6일

답변:

2017년 9월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by