Creating matrix outside for loop
1 回表示 (過去 30 日間)
古いコメントを表示
I need to create a matrix of variables and have the current code below. Is there anyway to do this without a for loop?
x = 0:.1:1;
y = 0:.1:1;
for i = 1:length(x)
for j = 1:length(y)
x2(i,j) = x(i);
y2(i,j) = y(j);
end
end
0 件のコメント
採用された回答
Star Strider
2017 年 2 月 19 日
編集済み: Star Strider
2017 年 2 月 19 日
Try this:
x2 = y'*ones(size(x));
y2 = ones(size(y'))*x;
EDIT —
The R2016b multiplication automatically does the expansion. Previous versions would require bsxfun calls:
x2 = bsxfun(@times, y', ones(size(x)));
y2 = bsxfun(@times, ones(size(y')), x);
5 件のコメント
Star Strider
2017 年 2 月 20 日
You do not need the loop.
This works:
U = sin(y1);
(I apologise for the delay.)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!