Documentation

cart2sph

Transform Cartesian coordinates to spherical

Syntax

[azimuth,elevation,r] = cart2sph(x,y,z)

Description

example

[方位,elevation,r] = cart2sph(x,y,z)transforms corresponding elements of the Cartesian coordinate arraysx,y, andzto spherical coordinates方位,elevation, andr.

Examples

collapse all

Convert the Cartesian coordinates defined by corresponding entries in the matricesx,y, andzto spherical coordinatesaz,el, andr. These points correspond to the eight vertices of a cube.

x = [1 1 1 1; -1 -1 -1 -1]
x =1 1 1 1 -1 -1 -1 -1
y = [1 1 -1 -1; 1 1 -1 -1]
y =1 1 -1 -1 1 1 -1 -1
z = [1 -1 1 -1; 1 -1 1 -1]
z =1 -1 1 -1 1 -1 1 -1
[az,el,r] = cart2sph(x,y,z)
az =0.7854 0.7854 -0.7854 -0.7854 2.3562 2.3562 -2.3562 -2.3562
el =0.6155 -0.6155 0.6155 -0.6155 0.6155 -0.6155 0.6155 -0.6155
r =1.7321 1.7321 1.7321 1.7321 1.7321 1.7321 1.7321 1.7321

Input Arguments

collapse all

Cartesian coordinates, specified as scalars, vectors, matrices, or multidimensional arrays.x,y, andzmust be the same size, or any of them can be scalar.

Data Types:single|double

Output Arguments

collapse all

Azimuth angle, returned as an array.方位is the counterclockwise angle in thex-yplane measured in radians from the positivex-axis.

Elevation angle, returned as an array.elevationis the elevation angle in radians from thex-yplane.

Radius, returned as an array.ris the distance from the origin to a point. The length units ofrare arbitrary, matching the units of the input arraysx,y, andz.

Algorithms

The mapping from three-dimensional Cartesian coordinates to spherical coordinates is

方位= atan2(y,x) elevation = atan2(z,sqrt(x.^2 + y.^2)) r = sqrt(x.^2 + y.^2 + z.^2)

The notation for spherical coordinates is not standard. For thecart2sphfunction,elevationis measured from thex-yplane. Notice that ifelevation = 0, the point is in thex-yplane. Ifelevation = pi/2, then the point is on the positivez-axis.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced before R2006a

Was this topic helpful?