# Matlab: Easy way of getting the standard basis vectors?

It seems like this should be easy, but I'm no expert and google isn't helping.

I would like an elegant way in Matlab of producing the standard ordered basis vectors for an n-dimensional space. For example, behaviour similar to the following:

>> [e1, e2] = SOB(2); >> e1 e1 = 1 0 >> e2 e2 = 0 1

I'm *hoping* for a 1-liner and don't really want to write a function for something so simple.

Thanks

## Answers

Why not

A = eye(N);

then A(:,i) is your i-th basis vector

Would two lines be ok? Create the identity matrix with EYE, copy vectors into a cell array using MAT2CELL, then distribute them with DEAL.

tmp = mat2cell(eye(N),N,ones(N,1)); [e1,e2,...,eN] = deal(tmp{:})

To obtain a *single* basis vector, say the k-th standard basis vector in N dimensions, you can use:

yourbasisvector = double(1:N == k)

1:N produces the vector 1 2 ... N, which == k element-wise tests for equality with k; double converts the logical values to numbers.

if you anonymous function, it is more convenient.

e = @(x) eye(size(A))(:,x);

If the size of A is 6 by 6, this returns 6 by 1 vector.

e(1) = [1;0;0;0;0;0]