How to merge two variables together in a table

조회 수: 1 (최근 30일)
Blue
Blue 2022년 2월 2일
답변: Cris LaPierre 2022년 2월 2일
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,

채택된 답변

Cris LaPierre
Cris LaPierre 2022년 2월 2일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by