MATLAB Answers

Naime
0

how many times specific number repeated in cell

Asked by Naime
on 15 Apr 2019
Latest activity Edited by Stephen Cobeldick on 15 Apr 2019
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

Sign in to comment.

1 Answer

Answer by madhan ravi
on 15 Apr 2019
Edited by madhan ravi
on 15 Apr 2019
 Accepted Answer

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

  0 Comments

Sign in to comment.