Pass elements to a different cell array given a specific condition

2 views (last 30 days)
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.

Answers (1)

dpb
dpb on 17 May 2014
Edited: dpb on 17 May 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'
>>

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!