# Matching unequal cell arrays

5 vues (au cours des 30 derniers jours)
Ronald le 3 Juin 2020
Commenté : Ronald le 5 Juin 2020
I have two unequal arrays and I want to match them to produce one array such that the unmatched cells are left empty i.e
A = {124,252,1252,225,598,999}
B = {598,'plant';
1252,'nil';
252,'blue'}
The result C;
C = {124,[];
252,'blue';
1252,'nil';
225,[];
598,'plant';
999,[]}
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (2)

A = {124,252,1252,225,598,999};
B = {598,'plant';...
1252,'nil';...
252,'blue'};
[~, Ai_member, Bi_member] = intersect([A{:}], [B{:,1}]);
C = [A(:), cell(numel(A), 1)];
[C{Ai_member, 2}] = B{Bi_member, 2}
##### 3 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Modifié(e) : TADA le 4 Juin 2020
In that case, ismember should do the trick
it returns a logical index (flags) and the matching indices in B
for matching cells, the logical index is true and the B index has the index of the match
and for unmatched cells both indices have the value zero
A = {124,252,252,1252,225,225,598,598,999};
B = {598,'plant';...
1252,'nil';...
252,'blue'};
[flags, Bidx] = ismember([A{:}], [B{:,1}]);
C = [A(:), cell(numel(A), 1)];
[C{flags, 2}] = B{bidx(bidx > 0), 2}
C =
9×2 cell array
{[ 124]} {0×0 double}
{[ 252]} {'blue' }
{[ 252]} {'blue' }
{[1252]} {'nil' }
{[ 225]} {0×0 double}
{[ 225]} {0×0 double}
{[ 598]} {'plant' }
{[ 598]} {'plant' }
{[ 999]} {0×0 double}
Ronald le 5 Juin 2020
This is really good. Thank you very much!

Connectez-vous pour commenter.

Stephen23 le 4 Juin 2020
Modifié(e) : Stephen23 le 4 Juin 2020
Using tables:
>> TA = cell2table(A(:))
TA =
Var1
____
124
252
252
1252
225
225
598
598
999
>> TB = cell2table(B'')
TB =
Var1 Var2
____ _______
598 'plant'
1252 'nil'
252 'blue'
>> T = outerjoin(TA,TB,'MergeKeys',true)
T =
Var1 Var2
____ _______
124 ''
225 ''
225 ''
252 'blue'
252 'blue'
598 'plant'
598 'plant'
999 ''
1252 'nil'
Note that the output rows are sorted by the joining key. To keep the original order:
>> [T,X] = outerjoin(TA,TB,'MergeKeys',true);
>> T(X,:) = T
T =
Var1 Var2
____ _______
124 ''
252 'blue'
252 'blue'
1252 'nil'
225 ''
225 ''
598 'plant'
598 'plant'
999 ''
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Data Types dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by