finding repetition numbers in array.
113 次查看(过去 30 天)
显示 更早的评论
A=[1;1;1;2;2;2;2;3;3;3]; %double
%I wanna known how many times 1,2,3 are exist in A matrix with orderly like that;
rep=[3;4;3]; %w.r.t A matrix (3 times 1, 4 times 2 and 3 times 3)
5 个评论
采纳的回答
Neuroscientist
2014-5-15
you can have something like this:
A=[1;1;1;2;2;2;2;3;3;3];
B = unique(A); % which will give you the unique elements of A in array B
Ncount = histc(A, B); % this willgive the number of occurences of each unique element
best NS
1 个评论
shubham gupta
2019-2-26
simple and clear explaination. thank you sir, now i am able to solve my problem.
更多回答(1 个)
Jos (10584)
2014-5-15
Your question title (finding repetition numbers) and your question text ("how many times exist") are open for ambiguity.
For A = [1 1 4 1 1 1] should the algorithm return [5 1], [5 0 0 1] or [2 1 3]?
A = [1 1 4 1 1 1]
% [5 1] case
R = histc(A,unique(A))
% [5 0 0 1] case
R = histc(A,1:max(A))
% [2 1 3] case
N = diff([0 find(diff(A)) numel(A)])
2 个评论
omran alshalabi
2022-8-28
hi, thank you for your detailed answer,
I have another question, can I get some case like,
% [1 4 1] case
MarKf
2022-10-22
removing duplicates then
A = [1 1 4 1 1 1];
b = A([true, diff(A)~=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!