How can I count the number of times a number appear in a vector?

599 ビュー (過去 30 日間)
Rafael Freire
Rafael Freire 2011 年 7 月 28 日
回答済み: PetterE 2024 年 5 月 3 日
How can I count the number of times a number appear in a vector? A=[ 1 2 3 1 1 6 3 4 2 2 1 1 1 ]
1 appears 7 times 2 appears 3 times 3 appears 3 times and on ... Best Regards
  3 件のコメント
indhu ravi
indhu ravi 2019 年 4 月 5 日
sir actually i have an matrix of about 0 0 0
1 0 1 but i should find the maximum occurence of same numbers i.e 0
1
Walter Roberson
Walter Roberson 2019 年 4 月 5 日
編集済み: Walter Roberson 2019 年 4 月 5 日
Rick showed how to count all the occurances of all of the numbers, and I showed a different approach at https://www.mathworks.com/matlabcentral/answers/12636-how-can-i-count-the-number-of-times-a-number-appear-in-a-vector#comment_312351

サインインしてコメントする。

採用された回答

Rick Rosson
Rick Rosson 2011 年 7 月 28 日
Please try the following:
A = [ 1 2 3 1 1 6 3 4 2 2 1 1 1 ];
x = unique(A);
N = numel(x);
count = zeros(N,1);
for k = 1:N
count(k) = sum(A==x(k));
end
disp([ x(:) count ]);
HTH.
Rick
  6 件のコメント
Walter Roberson
Walter Roberson 2021 年 5 月 16 日
I do not see any z in the code?
If A is not a vector, then I suggest
count(k) = sum(A(:) == x(k));
Enrique Sánchez
Enrique Sánchez 2021 年 9 月 26 日
If A is not a vector, then I suggest
count(k) = sum(A == x(k), 'all');

サインインしてコメントする。

その他の回答 (7 件)

Alexander
Alexander 2014 年 6 月 4 日
I realize this is old, but in case someone else stumbles upon it:
c = numel(find(a==1))
returns c = 7, so 1 occurs 7 times. Can embed into a for loop to find the occurrence of all members.
  3 件のコメント
Roshan
Roshan 2023 年 11 月 27 日
saved my life
Stephen23
Stephen23 2023 年 11 月 28 日
Simpler and more efficient:
c = nnz(a==1)

サインインしてコメントする。


Walter Roberson
Walter Roberson 2011 年 7 月 28 日
u = unique(A);
fprintf('%d appears %d times\n', [u; histc(A,u)].');
  2 件のコメント
hello_world
hello_world 2014 年 11 月 24 日
It gives me the following error:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Walter Roberson
Walter Roberson 2015 年 9 月 26 日
[u, ~, uidx] = unique(A(:));
counts = accumarray(uidx, 1);
fprintf('%d appears %d times\n', [u, counts].');

サインインしてコメントする。


Joseph Sottosanti
Joseph Sottosanti 2018 年 12 月 14 日
c = nnz(a==1)
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 12 月 14 日
This is good if you are wanting to count a specific value, but other code is needed if each value that occurs has to be tallyed like the original question asked.

サインインしてコメントする。


Steven Lord
Steven Lord 2019 年 4 月 5 日
You can use histcounts or histogram for this purpose. Let's generate some sample data, 100 integers between 1 and 10.
A = randi(10, 1, 100);
The edges should start at each unique value in A. We need to add one additional edge at the end, otherwise the last bin will count the two largest unique values contained in A, not just the largest. All bins but the last contain their left edge but not their right edge, while the last bin contains both its edges.
U = unique(A);
edges = [U, max(U)+1];
Now let's count. I'll put the results in a table array for easy reading.
counts = histcounts(A, edges);
results = table(U(:), counts(:), 'VariableNames', {'UniqueElements', 'Count'})
If you want to see the results graphically, use histogram instead of histcounts.
h = histogram(A, edges);
counts2 = h.Values;
isequal(counts2, counts) % true
If you want the bins to be centered on integer values (useful if your data is all integer values, as in the case in the original question) you can specify the 'integers' BinMethod instead of building the edges yourself. If you do this with histcounts you'd want to call it with two outputs so you receive both the bin counts and the vector of bin edges. Remember that the vector of bin edges will be one element longer than the vector of bin counts since bin edges are fence posts and bin counts are fence rails.
h = histogram(A, 'BinMethod', 'integers');
[counts3, edges3] = histcounts(A, 'BinMethod', 'integers')

indhu ravi
indhu ravi 2019 年 4 月 7 日
sir for example i have an matrix of 3x3
0 0 0
1 0 1
1 1 1
i should get the maximum occurrence of each row (i.e) 0
1
1
can anyone help me
  3 件のコメント
indhu ravi
indhu ravi 2019 年 4 月 7 日
i can't understand
madhan ravi
madhan ravi 2019 年 4 月 7 日
mode(matrix,2)

サインインしてコメントする。


Zhe Li
Zhe Li 2019 年 5 月 27 日
編集済み: Zhe Li 2019 年 5 月 27 日
A simple command that should work in most situations:
>> summary(categorical(A))
gives
1 2 3 4 6
6 3 2 1 1
But this is for display (or quick overview) only -- you cannot use the result without some copying and pasting. So if you want an array of these values you need to use some of the other methods found here.

PetterE
PetterE 2024 年 5 月 3 日
The simplest solution I can think of is probably
Au=unique(A);
occurances=sum(A(:)==Au(:)')
this will give you the list of unique numbers in Au and the occurances on the same indicies in the occurances vector.

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by