Append a column to a cell variable if two conditions verify
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Maria
am 13 Aug. 2014
Bearbeitet: Andrei Bobrov
am 13 Aug. 2014
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 Kommentare
Nathan
am 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.
Akzeptierte Antwort
Andrei Bobrov
am 13 Aug. 2014
Bearbeitet: Andrei Bobrov
am 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!