Multidimensional matrix from anonymous function
3 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Andrei Bobrov
on 16 Oct 2014
Edited: Andrei Bobrov
on 16 Oct 2014
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);
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!