
for文にテキストボックスの値を入れてループさせる方法を教えてください。
3 views (last 30 days)
Show older comments

私はfor文入る前にtest_dataの内容をそれぞれ代入し、for文で1つずつ回るのではないかと考えていました。しかしこれではY(from,to)の中にいれてしまうと(from,to)すべての場所に同じ値が入ってしまうという結果になりました。
Y =
0.0400 - 1.7992i 0.0400 - 1.7992i 0.0400 - 1.7992i 0.0400 - 1.7992i
0.0400 - 1.7992i 0.0400 - 1.7992i 0.0400 - 1.7992i 0.0400 - 1.7992i
0.0400 - 1.7992i 0.0400 - 1.7992i 0.0400 - 1.7992i 0.0400 - 1.7992i
0.0400 - 1.7992i 0.0400 - 1.7992i 0.0400 - 1.7992i 0.0400 - 1.7992i
Y(3,1)の場合は本来想定している結果はfrom3,to1の場合は0になるはずなのにほかの値と同じになってしまっています。
Y =
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0400 - 1.7992i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
なので、テキストボックスから値を取り出してfor文で回す方法が知りたいです。お願いします。
0 Comments
Accepted Answer
Atsushi Ueno
on 5 Jan 2022
Edited: Atsushi Ueno
on 5 Jan 2022
① Y(from,to) = 0.0400 - 1.7992i のfromとtoの添え字が無く、fromやtoの行列全体を指定する事になるので:Y([1,2,2,3,3,4,1,2,3,4], [2,1,3,2,4,3,1,2,3,4]) = 0.0400 - 1.7992iとなります。これだと、1度でY(1:4,1:4)全てにスカラ値を代入してしまいます。for文で何度繰り返しても同じです。
② from_dataからfrom, to, r, x, bにコピーすると、コピー先はそれぞれ10行1列のベクトルになります。

fromの各要素にアクセスする場合はfrom(3,1)の様に書きます。1次元ベクトルなのでfrom(3)も可能です。(詳細は線形インデックスを参照) したがって、下記の様にプログラムを変更すべきです。
for ii = 1:6
Y(from(ii), to(ii)) = 1 / (r(ii) + ii * x(ii)) + ii * b(ii) / 2;
end
More Answers (0)
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!