for文で連続する数字から変数を定義したい
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
for i = 1: 10
kazu = i;
name[ i ] = kazu;
name[0] 、name[ 1 ]のようにiの値ごとに定義される変数を作りたいのですが、
どのようにすればよいでしょうか。
0 commentaires
Réponse acceptée
Keita Abe
le 20 Oct 2022
配列でname(i)を定義するのではなく変数名自体に連番を振るにはevalを使えばできるみたいです。(推奨されないそうですが)
https://jp.mathworks.com/matlabcentral/answers/1729850-for?s_tid=answers_rc1-3_p3_MLT
for i = 1:10
kazu = i;
eval(sprintf('name%d=kazu;',i));
end
whos
2 commentaires
Atsushi Ueno
le 20 Oct 2022
推奨されるのは cell 配列です。
for i = 1:10
kazu = zeros(randi(5),randi(5)); % サイズがバラバラな行列を作成
name{i} = kazu;
end
name % 1行10列のcell配列。変数を10個作るのと同じ。型がバラバラでも各々格納してくれる
name{5}
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!