配列を構造体の形に変換

6 visualizzazioni (ultimi 30 giorni)
ash
ash il 7 Ago 2020
Risposto: michio il 7 Ago 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文を使う以外方法がわからず・・
実際には数万値の配列から構造体にしたく。

Risposte (1)

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

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!