필터 지우기
필터 지우기

How to find most occuring element in a matrix???

조회 수: 11 (최근 30일)
Neelabh
Neelabh 2012년 7월 16일
Hi..
I want to know that how can we find the most occurring element in the whole matrix (not row/column wise as by function mode)?
e.g if A = [1 2 3 7; 2 3 4 6; 4 3 5 6]
So most occurring element is 3 which occurs 3 times.
Thanks in advance....

채택된 답변

Wayne King
Wayne King 2012년 7월 17일
편집: Wayne King 2012년 7월 17일
A = [1 2 3 7
2 3 4 6
4 3 5 6];
[M,F] = mode(A(:));
M is the mode, the most often occuring element, 3. F tells how many times it occurs.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2012년 7월 17일

Image Analyst
Image Analyst 2012년 7월 17일
편집: Image Analyst 2012년 7월 17일
Of course use the mode(A(:)) function, as the others have pointed out, if you have integers. But if you have double or single numbers, not integers which you may have just used for simplification, then you need to be aware of the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F. In that case, you'll have to use hist() or histc() and look for the max value with max().
Let's just check to make sure. Tell me what you see when you do
whos A
in the command window right before you're about to check for the mode. Does it say int32 or double or something else? If it says double then what you get may depend on how you arrived at the values and that's why you need to understand the FAQ section I pointed you to.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by