How to make matlab count
古いコメントを表示
Hello Matlab community,
I wented to know is there a way to create a code to count how many times a number ouccers in a given matrix.
For example I would like to input:
x=input('Numeber')
=[1 2 3 4 5
1 2 3 5 6
7 2 4 3 5]
Is there a way for Matlab to count how many of each number has occured.
Please provide example Thank you so much.
採用された回答
その他の回答 (4 件)
Alex Mcaulley
2019 年 6 月 11 日
A =[1 2 3 4 5
1 2 3 5 6
7 2 4 3 5];
x=input('Number')
sum(A(:) == x)
Star Strider
2019 年 6 月 11 日
x = 5
M=[1 2 3 4 5
1 2 3 5 6
7 2 4 3 5]
Count = nnz(M == x)
producing:
Count =
3
Steven Lord
2019 年 6 月 11 日
1 投票
If you want the frequency of one particular element from the matrix, use the techniques madhan ravi, Alex Mcaulley, and Star Strider have given.
If you want just the counts for each unique element, use histcounts.
If you want a picture of the frequency data, use histogram. The graphics object histogram returns also has the bin count information in its properties, but if you don't need the picture I would use histcounts and skip the cost (in time and memory) of creating the picture.
4 件のコメント
madhan ravi
2019 年 6 月 11 日
編集済み: madhan ravi
2019 年 6 月 11 日
Steven but I have shown the way to find the counts for each unique element.
Steven Lord
2019 年 6 月 11 日
Yes, I see that now. Nice use of implicit expansion, by the way. I still would recommend histcounts or histogram as (among other things) it makes the code author's intent obvious (accumarray and sum are more general than histcounts) and uses the recommended function (in place of histc.)
madhan ravi
2019 年 6 月 12 日
Very true :)
Rainaire Hansford
2019 年 6 月 17 日
カテゴリ
ヘルプ センター および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!