Borrar filtros
Borrar filtros

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

11 visualizaciones (últimos 30 días)
Neelabh
Neelabh el 16 de Jul. de 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....

Respuesta aceptada

Wayne King
Wayne King el 17 de Jul. de 2012
Editada: Wayne King el 17 de Jul. de 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.

Más respuestas (2)

Walter Roberson
Walter Roberson el 17 de Jul. de 2012

Image Analyst
Image Analyst el 17 de Jul. de 2012
Editada: Image Analyst el 17 de Jul. de 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.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by