find element inside cell

조회 수: 1 (최근 30일)
NA
NA 2019년 3월 4일
댓글: NA 2019년 3월 4일
A=[1,2,3,4];
B=[5,6,7,8,9,10,11];
C=[12,13,14];
D=[14,15,16,17,18,19];
F=[20,21,22,23,24,25,26,27];
mes={[3,25],[14,10],[19,20],[26,4],[24,26],[1,8],[16,27],[22,11]};
check mes belongs to which group. [3,25] belongs to group A and F. [14,10] belongs to group C and B. [19,20] belongs to group D and F. [26,4] belongs to group F and A. [24,26] belongs to group F and F. [1,8] belongs to group A and B. [16,27] belongs to group D and F. [22,11] belongs to group F and B.
I want to have this result:
3 mes belongs to A.
3 mes belongs to B.
1 mes belongs to C.
2 mes belongs to D.
7 mes belongs to F.
I tried to use this code
for i=1:numel(mes)
if any(ismember(mes{i},A))
fprintf('\nbelongs to group A.\n');
end
if any(ismember(mes{i},B))
fprintf('\nbelongs to group B.\n');
end
if any(ismember(mes{i},C))
fprintf('\nbelongs to group C.\n');
end
if any(ismember(mes{i},D))
fprintf('\nbelongs to group D.\n');
end
if any(ismember(mes{i},F))
fprintf('\nbelongs to group F.\n');
end
end
but it does not give what I want.

채택된 답변

KSSV
KSSV 2019년 3월 4일
편집: KSSV 2019년 3월 4일
S = cell([],1) ;
A=[1,2,3,4];
B=[5,6,7,8,9,10,11];
C=[12,13,14];
D=[14,15,16,17,18,19];
F=[20,21,22,23,24,25,26,27];
mes={[3,25],[14,10],[19,20],[26,4],[24,26],[1,8],[16,27],[22,11]};
data = {A B C D F} ;
E = {'A' 'B' 'C' 'D' 'F'} ;
for i = 1:length(data)
count = 0 ;
for j = 1:length(mes)
[c,ia] = ismember(mes{j},data{i}) ;
if any(c)
count = count+1 ;
end
end
S{i} = sprintf('%d mes{%d} belongs to %s',count,i,E{i}) ;
end
  댓글 수: 6
KSSV
KSSV 2019년 3월 4일
Edited code again..
NA
NA 2019년 3월 4일
Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by