# cart2sphvec

Convert vector from Cartesian components to spherical representation

## Description

converts the components of a vector or set of vectors, `vs`

= cart2sphvec(`vr`

,`az`

,`el`

)`vr`

, from their
representation in a local Cartesian coordinate system to a *spherical basis
representation* contained in `vs`

. A spherical basis
representation is the set of components of a vector projected into a basis given by $$({\widehat{e}}_{az},{\widehat{e}}_{el},{\widehat{e}}_{R})$$. The orientation of a spherical basis depends upon its location on the
sphere as determined by azimuth, `az`

, and elevation,
`el`

.

## Examples

### Spherical Representation of Unit Z-Vector

Start with a vector in Cartesian coordinates pointing along the *z*-direction and located at 45° azimuth, 45° elevation. Compute its components with respect to the spherical basis at that point.

vr = [0;0;1]; vs = cart2sphvec(vr,45,45)

`vs = `*3×1*
0
0.7071
0.7071

## Input Arguments

`vr`

— Vector in Cartesian basis representation

3-by-1 column vector | 3-by-N matrix

Vector in Cartesian basis representation specified as a 3-by-1
column vector or 3-by-N matrix. Each column of `vr`

contains
the three components of a vector in the right-handed Cartesian basis *x,y,x*.

**Example: **`[4.0; -3.5; 6.3]`

**Data Types: **`double`

**Complex Number Support: **Yes

`az`

— Azimuth angle

scalar in range [–180,180]

Azimuth angle specified as a scalar in the closed range [–180,180].
Angle units are in degrees. To define the azimuth angle of a point
on a sphere, construct a vector from the origin to the point. The
azimuth angle is the angle in the *xy*-plane from
the positive *x*-axis to the vector's orthogonal
projection into the *xy*-plane. As examples, zero
azimuth angle and zero elevation angle specify a point on the *x*-axis
while an azimuth angle of 90° and an elevation angle of zero
specify a point on the *y*-axis.

**Example: **`45`

**Data Types: **`double`

`el`

— Elevation angle

scalar in range [–90,90]

Elevation angle specified as a scalar in the closed range [–90,90].
Angle units are in degrees. To define the elevation of a point on
the sphere, construct a vector from the origin to the point. The elevation
angle is the angle from its orthogonal projection into the *xy*-plane
to the vector itself. As examples, zero elevation angle defines the
equator of the sphere and ±90° elevation define the north
and south poles, respectively.

**Example: **`30`

**Data Types: **`double`

## Output Arguments

`vs`

— Vector in spherical basis

3-by-1 column vector | 3-by-N matrix

Spherical representation of a vector returned as a 3-by-1 column vector or 3-by-N matrix
having the same dimensions as `vs`

. Each column of
`vs`

contains the three components of the vector in the
right-handed $$({\widehat{e}}_{az},{\widehat{e}}_{el},{\widehat{e}}_{R})$$ basis.

## More About

### Spherical basis representation of vectors

Spherical basis vectors are a local set of basis vectors which point along the radial and angular directions at any point in space.

The spherical basis is a set of three mutually orthogonal unit vectors $$({\widehat{e}}_{az},{\widehat{e}}_{el},{\widehat{e}}_{R})$$ defined at a point on the sphere. The first unit vector points along lines of azimuth at constant radius and elevation. The second points along the lines of elevation at constant azimuth and radius. Both are tangent to the surface of the sphere. The third unit vector points radially outward.

The orientation of the basis changes from point to point on
the sphere but is independent of *R* so as you move
out along the radius, the basis orientation stays the same. The following
figure illustrates the orientation of the spherical basis vectors
as a function of azimuth and elevation:

For any point on the sphere specified by *az* and *el*,
the basis vectors are given by:

$$\begin{array}{ll}{\widehat{e}}_{az}\hfill & =-\mathrm{sin}(az)\widehat{i}+\mathrm{cos}(az)\widehat{j}\hfill \\ {\widehat{e}}_{el}\hfill & =-\mathrm{sin}(el)\mathrm{cos}(az)\widehat{i}-\mathrm{sin}(el)\mathrm{sin}(az)\widehat{j}+\mathrm{cos}(el)\widehat{k}\hfill \\ {\widehat{e}}_{R}\hfill & =\mathrm{cos}(el)\mathrm{cos}(az)\widehat{i}+\mathrm{cos}(el)\mathrm{sin}(az)\widehat{j}+\mathrm{sin}(el)\widehat{k}\text{}\text{.}\hfill \end{array}$$

Any vector can be written in terms of components in this basis as $$v={v}_{az}{\widehat{e}}_{az}+{v}_{el}{\widehat{e}}_{el}+{v}_{R}{\widehat{e}}_{R}$$. The transformations between spherical basis components and Cartesian components take the form

$$\left[\begin{array}{c}{v}_{x}\\ {v}_{y}\\ {v}_{z}\end{array}\right]=\left[\begin{array}{ccc}-\mathrm{sin}(az)& -\mathrm{sin}(el)\mathrm{cos}(az)& \mathrm{cos}(el)\mathrm{cos}(az)\\ \mathrm{cos}(az)& -\mathrm{sin}(el)\mathrm{sin}(az)& \mathrm{cos}(el)\mathrm{sin}(az)\\ 0& \mathrm{cos}(el)& \mathrm{sin}(el)\end{array}\right]\left[\begin{array}{c}{v}_{az}\\ {v}_{el}\\ {v}_{R}\end{array}\right]$$

.

and

$$\left[\begin{array}{c}{v}_{az}\\ {v}_{el}\\ {v}_{R}\end{array}\right]=\left[\begin{array}{ccc}-\mathrm{sin}(az)& \mathrm{cos}(az)& 0\\ -\mathrm{sin}(el)\mathrm{cos}(az)& -\mathrm{sin}(el)\mathrm{sin}(az)& \mathrm{cos}(el)\\ \mathrm{cos}(el)\mathrm{cos}(az)& \mathrm{cos}(el)\mathrm{sin}(az)& \mathrm{sin}(el)\end{array}\right]\left[\begin{array}{c}{v}_{x}\\ {v}_{y}\\ {v}_{z}\end{array}\right]$$.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Does not support variable-size inputs.

## Version History

**Introduced in R2013a**

## See Also

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

# Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)