配列を構造体の形に変換

8 views (last 30 days)
ash
ash on 7 Aug 2020
Answered: michio on 7 Aug 2020
以下の配列から
mx = [1 11;2 22;3 33;]
次のような構造体の形を作りたいです。
test(1).a = 1;test(2).a = 2;test(3).a = 3;
test(1).b = 11;test(2).b = 22;test(3).b = 33;
for文を使う以外方法がわからず・・
実際には数万値の配列から構造体にしたく。

Answers (1)

michio
michio on 7 Aug 2020
に一覧がありますが、数値配列から直接構造体に変換する関数はありません。
個人的には table 型を経由するのがシンプルな気がしますので以下にサンプルを記載します。
mx = [1 11;2 22;3 33;];
mx_table = array2table(mx,'VariableNames',["a","b"]);
test = table2struct(mx_table);
test(1).a
参考になりましたら。

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!