UnitVector[k]
gives the two-dimensional unit vector in the k
direction.
UnitVector[n,k]
gives the n-dimensional unit vector in the k
direction.
UnitVector
UnitVector[k]
gives the two-dimensional unit vector in the k
direction.
UnitVector[n,k]
gives the n-dimensional unit vector in the k
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 all close allBasic Examples (2)
Scope (2)
Applications (3)
Find the matrix for a "black box" linear operator:
m = Table[FourierDST[UnitVector[4, i], "IV"], {i, 4}]The matrix is equivalent to (though perhaps less efficient than) the "black box":
r = RandomReal[1, 4];FourierDST[r, "IV"]m.rThe matrix form allows you to use typical linear algebra functions:
Eigenvalues[m]p = RandomSample[Range[5]]mp = Map[UnitVector[5, #]&, p]mp.Range[5]UnitMatrix[{i_, j_}, {m_, n_}] := KroneckerProduct[UnitVector[m, i], UnitVector[n, j]]UnitMatrix[{2, 3}, {3, 3}]//MatrixFormProperties & Relations (1)
Possible Issues (1)
For very large dimensions n, the vector given by UnitVector may use a lot of memory:
v = UnitVector[1000000, 21];
ByteCount[v]An alternative is to use a SparseArray to represent the same thing:
s = SparseArray[{21} -> 1, 1000000]ByteCount[s]See Also
AngleVector IdentityMatrix KroneckerDelta Normalize RotationMatrix CirclePoints
Function Repository: RandomUnitVector
Related Guides
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_2026_unitvector, author="Wolfram Research", title="{UnitVector}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/UnitVector.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_unitvector, organization={Wolfram Research}, title={UnitVector}, year={2008}, url={https://reference.wolfram.com/language/ref/UnitVector.html}, note=[Accessed: 13-June-2026]}