How to find most occuring element in a matrix???
11 views (last 30 days)
Show older comments
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....
0 Comments
Accepted Answer
Wayne King
on 17 Jul 2012
Edited: Wayne King
on 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.
0 Comments
More Answers (2)
Image Analyst
on 17 Jul 2012
Edited: Image Analyst
on 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.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!