필터 지우기
필터 지우기

Multidimensional matrix from anonymous function

조회 수: 3 (최근 30일)
Min
Min 2014년 10월 16일
편집: Andrei Bobrov 2014년 10월 16일
I previously asked a question and I am still trying to calculate a equation with multiple variables of which some of them depend on another variable like this:
F=A(x)*(x*Ym(beta(x)*rho)+beta2(x)*Jm(beta(x)*rho)*cos(m*phi)*exp(1i*beta3(x)*z)
variables are x, rho, phi, and z, and also there are x dependent quantities like A, beta, beta2, beta3. I am not sure how I can calculate this equation in Matlab since if I use .* for terms such as beta3*z, it will not be right. I am fairly new to Matlab, so only workaround I can think of is to get for loop over x and calculate the equation for each x.
I have got an answer that I could use anonymous function:
F=@(x,rho,phi,z) A(x)*(x*Ym(beta(x)*rho)+beta2(x)*Jm(beta(x)*rho)*cos(m*phi)*exp(1i*beta3(x)*z)
However, I need to do more computations such as Fourier transform, I think I need to construct a multidimensional matrix (i.e. size(x)*size(rho)*size(phi)*size(z) matrix)
I am trying to avoid using for loop, as I think there could be a better way.
So, basically I am trying to construct multidimensional matrix with 4 variables (F=F(x,rho,phi,z)) and x,rho,phi,z all have different vector sizes.
Could anyone help me with this? Thanks.

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2014년 10월 16일
편집: Andrei Bobrov 2014년 10월 16일
In your case, for create multidimensional array (4D), try following code:
F=@(x,rho,phi,z) A(x).*(x.*Ym(beta(x).*rho)...
+beta2(x).*Jm(beta(x).*rho).*cos(m.*phi).*exp(1i*beta3(x).*z);
[i1,i2,i3,i4] = ndgrid(x,rho,phi,z);
out = F(i1,i2,i3,i4);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by