Append a column to a cell variable if two conditions verify

1 view (last 30 days)
I have a cell variable with 7000 rows and 14 columns. There are no repeated rows. Example with only 9 columns:
A={1994 1440 5510 '4911' '025537101' 'AEP' 1440 0 'AEP'
1994 1587 5510 '4941' '030411102' 'AWK' 1587 556 'AWK'
1994 1837 5510 '4924' '001204106' 'AGLT' 1837 969 'AGLT'}
And a cell variable with 60000 rows (no duplicates) and 3 columns. Example:
c1 c2 c3
B={1994 'ABCR' 101101
1994 'AEP' 30504
1994 'ADD' 40402
1994 'AGLT' 40404
1994 'ASIN' 100101
1994 'AWK' 80801}
If the first and the last columns of A match with the first and second columns of B (c1 and c2), I would like to add a new column to A with the values from the last column of B (c3).
My new A would be:
A={1994 1440 5510 '4911' '025537101' 'AEP' 1440 0 'AEP' 30504
1994 1587 5510 '4941' '030411102' 'AWK' 1587 556 'AWK' 80801
1994 1837 5510 '4924' '001204106' 'AGLT' 1837 969 'AGLT' 40404}
Thanks
  2 Comments
Nathan
Nathan on 13 Aug 2014
This is problematic and requires more information about what you would like to accomplish. does A or B contain doublicates? Do you want two additional columns period, but only write data into them if there is a match? Please be very specific so we can help.
Maria
Maria on 13 Aug 2014
Nathan ok. I will edit my answer and be more specific. Thank you.

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 13 Aug 2014
Edited: Andrei Bobrov on 13 Aug 2014
EDIT
[l1,i1] = ismember(A(:,end),B(:,2));
l2 = ismember([A{:,1}]',[B{:,1}]');
l3 = l1&l2;
A(l3,end+1) = B(i1(l3),3);
  2 Comments
Maria
Maria on 13 Aug 2014
Andrei it gives this error: Subscripted assignment dimension mismatch. But I will edit my question now and make it more undertandable. Thank you

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!