UnitVector

UnitVector[k]

gives the two-dimensional unit vector in the k^(th) direction.

UnitVector[n,k]

gives the n-dimensional unit vector in the k^(th) direction.

Details and Options

  • UnitVector[n,k] is a list of length n with a 1 in position k and 0s elsewhere.
  • UnitVector by default creates a vector containing exact integers.
  • The option WorkingPrecision can be used to specify the precision of vector components.

Examples

open allclose all

Basic Examples  (2)

The unit vector in the direction in two dimensions:

The unit vector in the direction in three dimensions:

Scope  (2)

A unit vector in dimension 100:

A machineprecision unit vector in the direction in two dimensions:

A 50digit-precision unit vector in the direction in two dimensions:

Applications  (3)

Find the matrix for a "black box" linear operator:

The matrix is equivalent to (though perhaps less efficient than) the "black box":

The matrix form allows you to use typical linear algebra functions:

p is a random permutation:

Get the permutation matrix:

Compute the unit matrices:

Properties & Relations  (1)

A random unit vector:

The length is equal to n:

There is a 1 in position k:

All other components are zero:

Possible Issues  (1)

For very large dimensions n, the vector given by UnitVector may use a lot of memory:

An alternative is to use a SparseArray to represent the same thing:

Wolfram Research (2007), UnitVector, Wolfram Language function, https://reference.wolfram.com/language/ref/UnitVector.html (updated 2008).

Text

Wolfram Research (2007), UnitVector, Wolfram Language function, https://reference.wolfram.com/language/ref/UnitVector.html (updated 2008).

CMS

Wolfram Language. 2007. "UnitVector." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2008. https://reference.wolfram.com/language/ref/UnitVector.html.

APA

Wolfram Language. (2007). UnitVector. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/UnitVector.html

BibTeX

@misc{reference.wolfram_2024_unitvector, author="Wolfram Research", title="{UnitVector}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/UnitVector.html}", note=[Accessed: 22-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_unitvector, organization={Wolfram Research}, title={UnitVector}, year={2008}, url={https://reference.wolfram.com/language/ref/UnitVector.html}, note=[Accessed: 22-December-2024 ]}