how many times specific number repeated in cell

1 回表示 (過去 30 日間)
NA
NA 2019 年 4 月 15 日
編集済み: Stephen23 2019 年 4 月 15 日
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 件のコメント
Stephen23
Stephen23 2019 年 4 月 15 日
編集済み: Stephen23 2019 年 4 月 15 日
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

サインインしてコメントする。

採用された回答

madhan ravi
madhan ravi 2019 年 4 月 15 日
編集済み: madhan ravi 2019 年 4 月 15 日
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 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by