How to covert rownames to the first variable in the table?

50 ビュー (過去 30 日間)
monkeyquant
monkeyquant 2023 年 1 月 25 日
回答済み: Star Strider 2023 年 1 月 25 日
I have a table with rownames (nuemric value) and want to convert it to a table with a newly added variable from the rownames.

採用された回答

Star Strider
Star Strider 2023 年 1 月 25 日
Try something like this —
T1 = array2table(randi(9, 5, 5));
T1.Properties.RowNames = {'1','2','3','4','5'} % Create Table With Row Names
T1 = 5×5 table
Var1 Var2 Var3 Var4 Var5 ____ ____ ____ ____ ____ 1 2 8 8 8 3 2 1 3 8 8 7 3 5 8 5 5 7 4 9 3 1 2 1 5 2 5 1 7 2
RN = T1.Properties.RowNames % Get Row Names
RN = 5×1 cell array
{'1'} {'2'} {'3'} {'4'} {'5'}
T1 = addvars(T1, cell2mat(RN), 'Before','Var1') % Convert Row Names To First Variable
T1 = 5×6 table
Var1_1 Var1 Var2 Var3 Var4 Var5 ______ ____ ____ ____ ____ ____ 1 1 2 8 8 8 3 2 2 1 3 8 8 7 3 3 5 8 5 5 7 4 4 9 3 1 2 1 5 5 2 5 1 7 2
T1.Properties.RowNames = {} % Delete Original Row Names
T1 = 5×6 table
Var1_1 Var1 Var2 Var3 Var4 Var5 ______ ____ ____ ____ ____ ____ 1 2 8 8 8 3 2 1 3 8 8 7 3 5 8 5 5 7 4 9 3 1 2 1 5 2 5 1 7 2
.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by