Multidimensional matrix from anonymous function

3 次查看(过去 30 天)
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