How can I pass 2-D meshgridded var into a 3-D matrix?
1 回表示 (過去 30 日間)
古いコメントを表示
Ezz El-din Abdullah
2017 年 7 月 21 日
コメント済み: Ezz El-din Abdullah
2017 年 7 月 21 日
I need to pass X and Y which are 2-D meshgridded matrices into c the 3-D matrix. where k is a constant and zv is a vector.
Here is my try:
for i = 1:M
c(:,:,i)=exp(1j*k/(2*zv(i))*(X2.^2+Y2.^2));
end
Thanks.
4 件のコメント
Steven Lord
2017 年 7 月 21 日
Show us the sizes of all of X2, Y2, c, and zv as well as the value of M as they are immediately before you enter the loop.
採用された回答
Guillaume
2017 年 7 月 21 日
編集済み: Guillaume
2017 年 7 月 21 日
Step 1: permute your vector so that it is along the 3rd dimension:
zv = permute(zv, [3 2 1]); %if zv is a column vector (M x 1)
%or
zv = permute(zv, [1 3 2]); %if zv is a row vector (1 x M)
Step 2: No need for loop, use memberwise operation + implicit expansion (if on R2016b or later):
c = exp(1j*k./(2*zv)) .* (X2.^2 + Y2.^2));
If on version < R2016b:
c = bsxfun(@times, exp(1j*k./(2*zv)), (X2.^2 + Y2.^2));
6 件のコメント
Guillaume
2017 年 7 月 21 日
As I wrote,
If on version < R2016b:
c = bsxfun(@times, exp(1j*k./(2*zv)), (X2.^2 + Y2.^2)); %after the permute
その他の回答 (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!