How to find most occuring element in a matrix???
11 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
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.
0 件のコメント
その他の回答 (2 件)
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!