how many times specific number repeated in cell
Show older comments
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 Comment
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
Accepted Answer
More Answers (0)
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!