Borrar filtros
Borrar filtros

How to merge two variables together in a table

1 visualización (últimos 30 días)
Blue
Blue el 2 de Feb. de 2022
Respondida: Cris LaPierre el 2 de Feb. de 2022
Hi, I have a quick question : I have an initial table T and I would like to merge the variables deph1 and deph2 into a single new variable called deph as shown in the desired output (T1).
% Initial table
set_no = [1, 2, 3]';
deph1 = [123, 125, 150]';
deph2 = [124, 126, 148]';
T = table(set_no, deph1, deph2)
T = 3×3 table
set_no deph1 deph2 ______ _____ _____ 1 123 124 2 125 126 3 150 148
% Desired output
set_no = [1, 1, 2, 2, 3, 3]';
deph = [123, 124, 125, 126, 150, 148]';
T1 = table(set_no, deph)
T1 = 6×2 table
set_no deph ______ ____ 1 123 1 124 2 125 2 126 3 150 3 148
Any ideas ?
Thank you,

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 2 de Feb. de 2022
Use stack.
% Initial table
set_no = [1, 2, 3]';
deph1 = [123, 125, 150]';
deph2 = [124, 126, 148]';
T = table(set_no, deph1, deph2)
T = 3×3 table
set_no deph1 deph2 ______ _____ _____ 1 123 124 2 125 126 3 150 148
% Desired output
T1 = stack(T,["deph1","deph2"])
T1 = 6×3 table
set_no deph1_deph2_Indicator deph1_deph2 ______ _____________________ ___________ 1 deph1 123 1 deph2 124 2 deph1 125 2 deph2 126 3 deph1 150 3 deph2 148

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by