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 Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by