Matlab: Easy way to get standard basis vectors?

It seems to be easy, but I'm not an expert, and google doesn't help.

I would like Matlab to have an elegant way to create standard ordered basis vectors for n-dimensional space. For example, the behavior is similar to the following:

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

  e1 =    1     0

>> e2

  e2 =    0     1

I hope for a 1-liner and don't want to write a function for something so simple.

thank

+5
source share
4 answers

Why not

A = eye(N); 

then this A(:,i)is your ith base vector

+17
source

? EYE, , MAT2CELL, DEAL.

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

To get a single base vector, say, the knth standard base vector in dimensions N, you can use:

yourbasisvector = double(1:N == k)

1:Ncreates a vector 1 2 ... Nthat == kchecks against elements to equal c k; doubleconverts boolean values ​​to numbers.

+1
source

if you are an anonymous function, this is more convenient.

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

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

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

All Articles