String matrix compare to get common in rows
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Touts Touts
am 26 Jun. 2019
Kommentiert: Touts Touts
am 28 Jun. 2019
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
0 Kommentare
Akzeptierte Antwort
Stephen23
am 28 Jun. 2019
Bearbeitet: Stephen23
am 28 Jun. 2019
>> 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 Kommentare
Stephen23
am 28 Jun. 2019
"...is it possible to ignore the empty values"
Of course, just add this line after X is defined:
X = X & ~cellfun(@isempty,A1);
Weitere Antworten (1)
Geoff Hayes
am 27 Jun. 2019
>> ismember(A1,A2)
ans =
1 0
0 0
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!