필터 지우기
필터 지우기

How can I pass 2-D meshgridded var into a 3-D matrix?

조회 수: 1 (최근 30일)
Ezz El-din Abdullah
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
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.
Ezz El-din Abdullah
Ezz El-din Abdullah 2017년 7월 21일
M is 250
X2 is 250x250
Y2 is 250x250
c is not defined before the loop but I want it to be 3D matrix (250x250x250)

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
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
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
Ezz El-din Abdullah
Ezz El-din Abdullah 2017년 7월 21일
Thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by