Effacer les filtres
Effacer les filtres

Pass elements to a different cell array given a specific condition

2 vues (au cours des 30 derniers jours)
Maria
Maria le 16 Mai 2014
Modifié(e) : dpb le 17 Mai 2014
I am working with matlab, and I have two different cell arrays with several elements. A primary and a secondary one. There is one element that is common in both cells, although the number of rows and order is not equal. What I would like is for X extra elements from the secondary cell to ‘pass’ to the primary one every time a condition is verified. The condition would be if column Y (from primary cell) and Z (from secondary cell) match. For instance:
Primary cell array:
ABC 970508 …
FED 970524 …
BAC 970601 …
IGH 970606 …
Secondary cell array
IGH FINANCE BANK1
FED HEALTH PILLS
ABC FINANCE BANK3
What I would like to get in the ‘new’ primary cell array:
ABC 970508 FINANCE BANK3
FED 970524 HEALTH PILLS
BAC 970601 …
IGH 970606 FINANCE BANK1
Can anyone help me? Thank you very much.

Réponses (1)

dpb
dpb le 17 Mai 2014
Modifié(e) : dpb le 17 Mai 2014
>> a={'ABC' '970508';'FED' '970524';'BAC' '970601';'IGH' '970606'};
>> b={'IGH' 'FINANCE BANK1';'FED' 'HEALTH PILLS';'ABC' 'FINANCE BANK3'};
>> [ia,ib]=ismember(a(:,1),b(:,1));
>> a(ia,end+1)=b(ib(ib>0),2)
a =
'ABC' '970508' 'FINANCE BANK3'
'FED' '970524' 'HEALTH PILLS'
'BAC' '970601' []
'IGH' '970606' 'FINANCE BANK1'
>>

Catégories

En savoir plus sur Financial Toolbox 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