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.



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]

Need Your Help

geocoder.getFromLocation(); Timed out waiting for response from server

android google-maps google-geocoder

I have an Android App launched to market over 6 months, which uses geocoder.getFromLocation() to "translate" latitude and longitude to human readable addresses:

Scala - How to compile code from an external file at runtime?

scala scala-compiler external-dependencies

I want to design a Scala program that accepts Scala files as parameters which can customize the execution of the program. In particular, I want to supply at runtime files that contain implementati...