Filter löschen
Filter löschen

Multidimensional matrix from anonymous function

3 Ansichten (letzte 30 Tage)
Min
Min am 16 Okt. 2014
Bearbeitet: Andrei Bobrov am 16 Okt. 2014
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.

Antworten (1)

Andrei Bobrov
Andrei Bobrov am 16 Okt. 2014
Bearbeitet: Andrei Bobrov am 16 Okt. 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);

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by