Create new variable if elements of different variables match in Matlab
Show older comments
I have 3 cell arrays: A that has 3 columns and 5000 rows. B that has 5 and 60 columns. C that has 4 columns and 500 rows. Sample of each varaible:
A= { 12 1999 41.2
13 1999 47.7
16 1999 52.7}
B= { 16 452 8 407 31
1037 110 14 873 93
1255 881 20 16 69}
C= { 13 1997 1999 2000
141 1997 1999 2000
174 1997 1999 2000}
If a value of the 1st column of A matches any value of the 1st column of B , I would like to add to A, columns 1, 4 and 5 of B plus a column only with the value 'one' . In case a value of the 1st column of A matches a value of the 1st column of C, I would like to add to A, columns 1 and 4 of C and an extra column with zeroes.
So the output would be:
Output= { 12 1999 41.2
13 1999 47.7 13 2000 0 %match between A & C
16 1999 52.7 16 407 31 1} %match between A & B
Can someone help me? Thank you.
1 Comment
Azzi Abdelmalek
on 4 Aug 2014
This is not clear
Accepted Answer
More Answers (1)
Ben11
on 4 Aug 2014
Here is something to get you started. It's not the most elegant but I think it answers your question. Note that I added NaN in some case in order to keep dimensions consistent.
clear
clc
A= { 12 1999 41.2
13 1999 47.7
16 1999 52.7};
B= { 16 452 8 407 31
1037 110 14 873 93
1255 881 20 16 69};
C= { 13 1997 1999 2000
141 1997 1999 2000
174 1997 1999 2000};
A = cell2mat(A); % transform cell arrays into numeric arrays
B = cell2mat(B);
C = cell2mat(C);
CommonValuesIndicesBinA= find(ismember(A(:,1),B(:,1))) % find indices in A corresponding to matching values in B.
CommonValuesIndicesAinB= find(ismember(B(:,1),A(:,1))) % find indices in B corresponding to matching values in A.
CommonValuesIndicesCinA = find(ismember(A(:,1),C(:,1))); % find indices in A corresponding to matching values in C.
CommonValuesIndicesAinC = find(ismember(C(:,1),A(:,1))); % find indices in C corresponding to matching values in A.
A(CommonValuesIndicesBinA,4:7) = [ B(CommonValuesIndicesAinB,1) B(CommonValuesIndicesAinB,4) B(CommonValuesIndicesAinB,5) 1]; % Add values in A with those you want from B
A(CommonValuesIndicesCinA,4:7) = [ C(CommonValuesIndicesAinC,1) C(CommonValuesIndicesAinC,4) 0 NaN]; % Add values in A with those you want from C. Add NaN at last entry to keep dimensions consistent
7 Comments
Maria
on 4 Aug 2014
Ben11
on 4 Aug 2014
Oh that's weird; I tried it on another computer and it works. What version are you using?
Maria
on 4 Aug 2014
Ben11
on 4 Aug 2014
ok. Then I'm puzzled I guess you copied/pasted the exact code right?
Maria
on 5 Aug 2014
Maria
on 5 Aug 2014
Ben11
on 5 Aug 2014
I modified the last part with this and I think it should work
for k =1:size(CommonValuesIndicesBinA,1)
p = CommonValuesIndicesBinA(k);
A(p,4:7) = [ B(CommonValuesIndicesAinB(k),1) B(CommonValuesIndicesAinB(k),4) B(CommonValuesIndicesAinB(k),5) 1]; % Add values in A with those you want from B
end
for kk =1:size(CommonValuesIndicesCinA,1)
pp = CommonValuesIndicesCinA(kk);
A(pp,4:7) = [ C(CommonValuesIndicesAinC(kk),1) C(CommonValuesIndicesAinC(kk),4) 0 NaN]; % Add values in A with those you want from C. Add NaN at last entry to keep dimensions consistent
end
Categories
Find more on Creating and Concatenating Matrices 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!