Filter löschen
Filter löschen

Pass elements to a different cell array given a specific condition

2 Ansichten (letzte 30 Tage)
Maria
Maria am 16 Mai 2014
Bearbeitet: dpb am 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.

Antworten (1)

dpb
dpb am 17 Mai 2014
Bearbeitet: dpb am 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'
>>

Kategorien

Mehr zu Financial Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by