How can I insert data into rows without looping and logical indexing?
Show older comments
I have two tables. table1 is 10*5 and table2 is 5*10.
table1 = table;
table1.var1 = [1; 1; 3; 3; 6; 1; 1; 3; 3; 6];
table1.var2 = [1; 2; 2; 3; 6; 1; 2; 2; 3; 6];
table1.var3 = [0; 2; 3; 4; 6; 0; 2; 3; 4; 6];
table1.var4 = [0; 2; 1; 5; 6; 0; 2; 1; 5; 6];
table1.var5 = [0; 2; 8; 7; 6; 0; 2; 8; 7; 6];
table2 = table;
table2.var1 = [1; 1; 3; 4; 5];
table2.var2 = [1; 2; 2; 5; 5];
table2.var3 = [1; 3; 3; 4; 5];
table2.var4 = [1; 2; 3; 4; 5];
table2.var5 = [1; 2; 4; 4; 5];
table2.var6 = [1; 8; 3; 4; 5];
table2.var7 = [1; 9; 3; 4; 5];
table2.var8 = [1; 7; 3; 4; 5];
table2.var9 = [1; 2; 3; 4; 5];
table2.var10 = [0; 1; 2; 3; 4];
test = table2(ismember(...
[table2.var1, table2.var2],...
[table1.var1, table1.var2], 'rows'), :);
How can I take var7 and var8 from test and input them in the matching rows from table1?
the output should look something like:
table1.var1 = [1; 1; 3; 3; 6; 1; 1; 3; 3; 6];
table1.var2 = [1; 2; 2; 3; 6; 1; 2; 2; 3; 6];
table1.var3 = [0; 2; 3; 4; 6; 0; 2; 3; 4; 6];
table1.var4 = [0; 2; 1; 5; 6; 0; 2; 1; 5; 6];
table1.var5 = [0; 2; 8; 7; 6; 0; 2; 8; 7; 6];
table1.var6 = [1; 9; 3; 0; 0; 1; 9; 3; 0; 0];
table1.var7 = [1; 7; 3; 0; 0; 1; 7; 3; 0; 0];
I can complete this with looping and logicaly indexing but would like to optimze for speed. Is there a faster way?
Can I directly insert table2.var7 and table2.var8 into table1 at every row were [table2.var1, table2.var] are the same rows of [table1.var1, table1.var2]? If so, how?
Accepted Answer
More Answers (0)
Categories
Find more on Tables 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!