Move a portion of one table into another, smaller, table.
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Steve K
el 14 de Oct. de 2022
Comentada: Steve K
el 17 de Oct. de 2022
I have two large csv files which I have imported using readtable. The contain related data but one has many more rows than the other. I have a common column between the two. I want to pull variables from the larger one at instances where the common column matches between the two tables. Forgive my syntax a bit here since I do not have matlab on the computer I am writing the question from.
table_A.VariableNames={'x' 'y' 'z'}
table_A=[.98 .3 .9;
.94 .1 .3;
.67 .1 .3;
.56 .7 .4;
.72 .4 .3;
.64 .6 .2]
table_B.VariableNames={'x' 'q' 'e'}
table_B=[.98 -.201 -.239;
.72 -2.18 -0.05;
.67 -.228 -1.28]
So in the given example I want to take the values of y and z, in table A, where x values match those in table B, and append them onto table B. I attempted to create a logical index vector with;
X_needed=table_A.x==table_B.x
I would then use that index vector to create a table with the desired values of y and z to append onto table B.
However, when I try this I get the error that "matrix dimensions must agree". I think I can reach the solution with a loop, but there must be a better way to solve this.
Thanks for any help provided.
0 comentarios
Respuesta aceptada
Voss
el 14 de Oct. de 2022
table_A = array2table([ ...
.98 .3 .9;
.94 .1 .3;
.67 .1 .3;
.56 .7 .4;
.72 .4 .3;
.64 .6 .2], ...
'VariableNames',{'x' 'y' 'z'})
table_B = array2table([ ...
.98 -.201 -.239;
.72 -2.18 -0.05;
.67 -.228 -1.28], ...
'VariableNames',{'x' 'q' 'e'})
[~,idx] = ismember(table_B.x,table_A.x);
table_B{:,{'y' 'z'}} = table_A{idx,{'y' 'z'}};
disp(table_B);
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!