({{, (RefLink[UnitVector, paclet:ref/UnitVector])[k] 
gives the two-dimensional unit vector in the k(^th) direction.}, {, (RefLink[UnitVector, paclet:ref/UnitVector])[n,k]
gives the n-dimensional unit vector in the k(^th) direction.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[UnitVector, paclet:ref/UnitVector])[n,k] is a list of length n with a 1 in position k and 0s elsewhere.

(RefLink[UnitVector, paclet:ref/UnitVector]) by default creates a vector containing exact integers.

The option (RefLink[WorkingPrecision, paclet:ref/WorkingPrecision]) can be used to specify the precision of vector components.

OpenersPrimaryExamplesSection(Spacer1)Examples  (9)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

The unit vector in the x direction in two dimensions:

UnitVector[1]

The unit vector in the y direction in three dimensions:

UnitVector[3,2]

OpenersExampleSection1(Spacer1)Scope  (2)

A unit vector in dimension 100:

UnitVector[100, 4]

A machine-precision unit vector in the y direction in two dimensions:

N[UnitVector[2]]

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

N[UnitVector[2], 50]

OpenersExampleSection3(Spacer1)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.r

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

Eigenvalues[m]

p is a random permutation:

p = RandomSample[Range[5]]

Get the permutation matrix:

mp = Map[UnitVector[5, #]&, p]

mp.Range[5]

Compute the unit matrices:

UnitMatrix[{i_,j_},{m_,n_}]:=KroneckerProduct[UnitVector[m,i],UnitVector[n,j]]

UnitMatrix[{2,3},{3,3}]//MatrixForm

OpenersExampleSection4(Spacer1)Properties & Relations  (1)

A random unit vector:

n = RandomInteger[{1, 1000}];
k = RandomInteger[{1,n}];
v = UnitVector[n, k];

The length is equal to n:

Length[v] == n

There is a 1 in position k:

Position[v, 1] == {{k}}

All other components are zero:

n - Count[v, 0]

OpenersExampleSection5(Spacer1)Possible Issues  (1)

For very large dimensions n, the vector given by (RefLink[UnitVector, paclet:ref/UnitVector]) may use a lot of memory:

v = UnitVector[1000000,21];
ByteCount[v]

An alternative is to use a (RefLink[SparseArray, paclet:ref/SparseArray]) to represent the same thing:

s = SparseArray[{21}->1, 1000000]

ByteCount[s]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[AngleVector, paclet:ref/AngleVector])  ▪  (RefLink[IdentityMatrix, paclet:ref/IdentityMatrix])  ▪  (RefLink[KroneckerDelta, paclet:ref/KroneckerDelta])  ▪  (RefLink[Normalize, paclet:ref/Normalize])  ▪  (RefLink[RotationMatrix, paclet:ref/RotationMatrix])  ▪  (RefLink[CirclePoints, paclet:ref/CirclePoints])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Operations on Vectors, paclet:guide/OperationsOnVectors])}})}})