how to compare 2 or more cell arrays?

조회 수: 47 (최근 30일)
chocho
chocho 2017년 3월 9일
댓글: Bharath Sagar 2020년 2월 11일
Hi, i have 2 cells(cell1 and cell2) of size 628*1 and 244*1 respectively, and i want to compare the elements of cell 1 with the elements of cell 2 and return only the matched ones from cell1 and delete the rests
cell1{k,1}={' *tcaa-a6-2677*';'tcca-a6-2681';' *tcca-aa-3566*';............};
%
cell2{k,1}={'TCCA-A6-2671-11A-01R-1758-07';' *TCCA-A6-2677*-11A-01R-0826-07';'TCCA-AA-3520-11A-01R-0721-07'; 'TCCA-AA-3528-11A-01R-1758-07'; *TCCA-AA-3566*-01A-01R-0125-07';.................}
Hints: i tried strncmpi and length, but because of size issue i get confused about which Command statement is better in my case.

채택된 답변

Stephen23
Stephen23 2017년 3월 9일
편집: Stephen23 2017년 3월 9일
After removing the asterisks, space characters, and fixing the first element of C1, this seems to do the job:
>> C1 = {'tcca-a6-2677';'tcca-a6-2681';'tcca-aa-3566'};
>> C2 = {'TCCA-A6-2671-11A-01R-1758-07';'TCCA-A6-2677-11A-01R-0826-07';'TCCA-AA-3520-11A-01R-0721-07';'TCCA-AA-3528-11A-01R-1758-07';'TCCA-AA-3566-01A-01R-0125-07'};
>> fun = @(s)any(strncmpi(s,C2,numel(s)));
>> out = C1(cellfun(fun,C1))
out =
'tcca-a6-2677'
'tcca-aa-3566'
  댓글 수: 8
JR
JR 2018년 10월 8일
편집: JR 2018년 10월 8일
Awesome. Thanks Stephen, I had no idea about function handles using @ command.
Bharath Sagar
Bharath Sagar 2020년 2월 11일
Hello,
I have a similar need. I have 2 cells(cell1 and cell2) of size 72*1 and 110*1 respectively, and i want to compare the elements of cell 1 with the elements of cell 2 and return only the matched ones from cell1 and delete the rest.
The above mentioned solution is not working for me.
Can you please help me out with this ?

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

추가 답변 (1개)

KSSV
KSSV 2017년 3월 9일
편집: KSSV 2017년 3월 9일
cell1 = {'apple' 'boy' 'cat' 'dog' 'elephant'} ;
cell2 = {'apple' 'ball' 'cut' 'dog' 'egg' 'frog' 'gun'} ;
iwant = intersect(cell1,cell2) ;
  댓글 수: 9
chocho
chocho 2017년 3월 9일
i think you match element by element but i want to check every element in cell1 with all the elements in cell 2.
chocho
chocho 2017년 3월 9일
편집: per isakson 2017년 3월 10일
cell1{k,1}={' *tcaa-a6-2677*';'tcca-a6-2681';' *tcca-aa-3566*';............};
%
cell2{k,1}={'TCCA-A6-2671-11A-01R-1758-07';' *TCCA-A6-2677*-11A-01R-0826-07';'TCCA-AA-3520-11A-01R-0721-07'; 'TCCA-AA-3528-11A-01R-1758-07'; *TCCA-AA-3566*-01A-01R-0125-07';.................}
in this example elements 1 & 3 of cell 1 are match with the elements 2 & 5 of cell2

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by