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 commentaires

Yao Li
Yao Li le 15 Mai 2014
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?
sermet
sermet le 15 Mai 2014
Modifié(e) : sermet le 15 Mai 2014
I wanna determine the repetition times of each number in A matrix for example 1 repeated 3 times, 2 repeated 4 times and so on.
Jos (10584)
Jos (10584) le 15 Mai 2014
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
Yao Li le 15 Mai 2014
He accepted Neuroscientist's answer below. Seems [5,1] is the correct answer.
Jos (10584)
Jos (10584) le 15 Mai 2014
Sure Yao, but the ambiguity remains ...

Connectez-vous pour commenter.

 Réponse acceptée

Neuroscientist
Neuroscientist le 15 Mai 2014

7 votes

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 commentaire

shubham gupta
shubham gupta le 26 Fév 2019
simple and clear explaination. thank you sir, now i am able to solve my problem.

Connectez-vous pour commenter.

Plus de réponses (1)

Jos (10584)
Jos (10584) le 15 Mai 2014

16 votes

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 commentaires

omran alshalabi
omran alshalabi le 28 Août 2022
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])
b = 1×3
1 4 1

Connectez-vous pour commenter.

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by