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
채택된 답변
추가 답변 (3개)
Star Strider
2017년 9월 6일
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
2017년 9월 6일
Walter Roberson
2017년 9월 6일
if nnz(A == max(A(:))) > 1
...
end
Star Strider
2017년 9월 6일
@Walter — Thank you.
Number of maximum values:
numMax=sum(A(:) == max(A(:)));
if numMax > 1
%Do your thing
end
댓글 수: 2
Stephen23
2017년 9월 6일
@Jane Ak: Star Strider already explained that in their answer from three hours ago.
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!