Vectorization in more than two dimensions
古いコメントを表示
Is there a way to use vectorized operations in more than two dimensions? For example, in the following code I want to create a three-dimensional matrix based off a function in three variables. I am able to vectorize in two dimensions using the transpose function, but for the third dimension I use a for loop. Is there a way to eliminate the loop?
x=1:1000;x=x';
y=1:586;
z=1:247;
U=zeros(length(x),length(y),length(z));
for cnt=1:length(z)
U(:,:,cnt)=(x.^2+y.^3*z(cnt)).*exp(x*z(cnt).^2).*besselj(0,y);
end
1 件のコメント
Nicole Brimhall
2020 年 11 月 25 日
回答 (1 件)
Walter Roberson
2020 年 11 月 25 日
編集済み: Walter Roberson
2020 年 11 月 25 日
X = reshape(1:5, [], 1, 1);
Y = reshape(1:6, 1, [], 1);
Z = reshape(1:3, 1, 1, []);
U = (X.^2+Y.^3.*Z).*exp(X.*Z.^2).*besselj(0,Y);
size(U)
Need R2016b or later.
2 件のコメント
Jan
2020 年 11 月 25 日
This is much more efficient then expanding the arrays. besselj is expensive. Then expanding the argument wastes a lot of time with calculating the results for the same inputs.
Nicole Brimhall
2020 年 11 月 25 日
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!