finding repetition numbers in array.
이전 댓글 표시
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
Yao Li
2014년 5월 15일
I'm not sure I've understood your question. My current understanding is you have a matrix A, and wanna calculate the array rep. Am I right?
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]?
Yao Li
2014년 5월 15일
He accepted Neuroscientist's answer below. Seems [5,1] is the correct answer.
Jos (10584)
2014년 5월 15일
Sure Yao, but the ambiguity remains ...
채택된 답변
추가 답변 (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
removing duplicates then
A = [1 1 4 1 1 1];
b = A([true, diff(A)~=0])
카테고리
도움말 센터 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!