Filter löschen
Filter löschen

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

11 Ansichten (letzte 30 Tage)
Neelabh
Neelabh am 16 Jul. 2012
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....

Akzeptierte Antwort

Wayne King
Wayne King am 17 Jul. 2012
Bearbeitet: Wayne King am 17 Jul. 2012
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.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 17 Jul. 2012

Image Analyst
Image Analyst am 17 Jul. 2012
Bearbeitet: Image Analyst am 17 Jul. 2012
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.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by