配列を構造体の形に変換

6 vues (au cours des 30 derniers jours)
ash
ash le 7 Août 2020
Réponse apportée : michio le 7 Août 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文を使う以外方法がわからず・・
実際には数万値の配列から構造体にしたく。

Réponses (1)

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

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!