Append a column to a cell variable if two conditions verify
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Nathan
2014-8-13
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.
采纳的回答
Andrei Bobrov
2014-8-13
编辑:Andrei Bobrov
2014-8-13
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!