String matrix compare to get common in rows

Deal all, i have a problem to compare two string matrix :
A1 = {'AA', 'b' ; 'cc', 'ff'}
A2 = {'ee', 'AA' ; 'hhh', 'm'}
strcmp(A1,A2)
I get
A1 =
'AA' 'b'
'cc' 'ff'
A2 =
'ee' 'AA'
'hhh' 'm'
ans =
0 0
0 0
But i need to get
'AA' is common between A1 iand A2 in the row 1
Best regard

 채택된 답변

Stephen23
Stephen23 2019년 6월 28일
편집: Stephen23 2019년 6월 28일

0 개 추천

>> A1 = {'AA', 'b' ; 'cc', 'ff' ; 'tt', 'kk'; 'XX', 'b'; 'AA', 'b'};
>> A2 = {'ee', 'AA' ; 'hhh', 'm'; 'kk', 'o'; 'ee', 'XX'; 'AA', 'b'};
>> X = cell2mat(cellfun(@ismember,num2cell(A1,2),num2cell(A2,2),'uni',0));
>> [R,~] = find(X);
>> [~,Y] = sort(R);
>> C = [A1(X),num2cell(R)].';
>> fprintf('''%s'' common at row %d\n',C{:,Y})
'AA' common at row 1
'kk' common at row 3
'XX' common at row 4
'AA' common at row 5
'b' common at row 5

댓글 수: 3

Its work parfectely, please is it possible to ignore the empty values
A1 = {'AA', '', 'cc', 'ff' ; 'tt', 'kk', 'XX', 'ee'}
A2 = {'ee', 'AA', 'hhh', ''; 'kk', 'o', 'ee', 'XX'}
X = cell2mat(cellfun(@ismember,num2cell(A1,2),num2cell(A2,2),'uni',0));
[R,~] = find(X);
[~,Y] = sort(R);
C = [A1(X),num2cell(R)].';
fprintf('''%s'' common at row %d\n',C{:,Y})
'AA' common at row 1
'' common at row 1
'kk' common at row 2
'XX' common at row 2
'ee' common at row 2
ignore this cas
'' common at row 1
Thanks a lot
"...is it possible to ignore the empty values"
Of course, just add this line after X is defined:
X = X & ~cellfun(@isempty,A1);
Touts Touts
Touts Touts 2019년 6월 28일
Thanks a lot dear Stephen Cobeldick

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

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2019년 6월 27일

0 개 추천

Touts - consider using ismember to check to see if any string of A1 matches with any string of A2
>> ismember(A1,A2)
ans =
1 0
0 0

댓글 수: 5

Touts Touts
Touts Touts 2019년 6월 27일
Its work, please, my real problem have a larg matrix, so is there any way to get
The common 'AA' and its rows lik this
'AA' : 1
Best regard
Geoff Hayes
Geoff Hayes 2019년 6월 27일
So if 'AA' is in the second row, do you expect to see
'AA' : 2
? What happens if 'AA' is in more than one row?
Not only the 'AA', but all the common string between A1 iand A2
A1 = {'AA', 'b' ; 'cc', 'ff' ; 'tt', 'kk'}
A2 = {'ee', 'AA' ; 'hhh', 'm'; 'kk', 'o'}
ismember(A1,A2)
so
A1 =
'AA' 'b'
'cc' 'ff'
'tt' 'kk'
A2 =
'ee' 'AA'
'hhh' 'm'
'kk' 'o'
ans =
1 0
0 0
0 1
Please i need to get
'AA' is common between A1 and A2 in the row 1
'kk' is common between A1 and A2 in the row 3
Best regard
Stephen23
Stephen23 2019년 6월 28일
편집: Stephen23 2019년 6월 28일
@Touts Touts: repeating and clarifying Geoff Hayes's questions, which you did not answer:
  1. what happens if 'AA' occurs on different rows?
  2. what happens if 'AA' occurs multiple times within one matrix?
Please, i need all the common between A1 and A2 and their rows
A1 = {'AA', 'b' ; 'cc', 'ff' ; 'tt', 'kk'; 'XX', 'b'; 'AA', 'b'}
A2 = {'ee', 'AA' ; 'hhh', 'm'; 'kk', 'o'; 'ee', 'XX'; 'AA', 'b'}
so
A1 =
'AA' 'b'
'cc' 'ff'
'tt' 'kk'
'XX' 'b'
'AA' 'b'
A2 =
'ee' 'AA'
'hhh' 'm'
'kk' 'o'
'ee' 'XX'
'AA' 'b'
I need
  • 'AA' common at row N° :1
  • 'kk' common at row N° :3
  • 'XX' common at row N° :4
  • 'AA' common at row N° :5
  • 'b' common at row N° :5
Any common at any row
Best regard

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

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

제품

질문:

2019년 6월 26일

댓글:

2019년 6월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by