How to merge two variables together in a table

1 view (last 30 days)
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,

Accepted Answer

Cris LaPierre
Cris LaPierre on 2 Feb 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

More Answers (0)

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!