how many times specific number repeated in cell

1 visualizzazione (ultimi 30 giorni)
E=[1 2;1 5;2 3;2 4;2 5;3 4;4 5;4 7;4 9;5 6;6 11;6 12;6 13;7 8;7 9;9 10;9 14;10 11;12 13;13 14];
N=(1:max(E(:)))';
A=[];
for i=1:max(E(:))
if sum(any(ismember(E,N(i)),2),1)==2
A(i)=N(i);
end
end
A(A==0)=[]; %specific number
B={[1,2,5],[2,3,4],[1,2,4,5],[1,2,4,5,6,9,10,11],[1,2,4,5,6,9,13,14]}; % want to see how many times
for i = 1:numel(B)
count = 0 ;
for j = 1:numel(A)
[c,ia] = ismember(B{i}, A(j)) ;
if any(c)
count = count+1 ;
end
end
number_of_repeat{i} = count ;
end
I want have, how many times each elamnet of A is repeated in B
I used above code but does gives me what I want
result should be:
result=[1,4;3,1;10,1;11,0;12,0;14,1]
means that 1 is repeated 4 times. 3 is repeated 1 times.
  1 Commento
Stephen23
Stephen23 il 15 Apr 2019
Modificato: Stephen23 il 15 Apr 2019
You can generate A much more efficiently using a histogram function:
>> N = 1:max(E(:));
>> X = hist(E(:),N);
>> A = N(X==2)
A =
1 3 10 11 12 14

Accedi per commentare.

Risposta accettata

madhan ravi
madhan ravi il 15 Apr 2019
Modificato: madhan ravi il 15 Apr 2019
Note: You have a mistake in your desired result 11 appears once.
v=[B{:}]; % assuming each contents of B is a vector if not make them as one and then proceed
result=[A(:),sum(v==unique(A).',2)]
Gives:
result =
1 4
3 1
10 1
11 1
12 0
14 1

Più risposte (0)

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by