0

Is there any way to simplify the following two line process into a single line?

e = eye(n);
e = e(:,k);

Note: This gets the k-th n-dimensional standard basis vector.

Michael Howlard
  • 217
  • 2
  • 8
  • Can I ask why? I fear that collapsing this into a single line has the potential to reduce readability, without any performance enhancement – Matt Taylor Sep 29 '15 at 10:46
  • I was just curious to see if there is a more elegant way to do this - however if it will reduce readability this will suffice! Thanks. – Michael Howlard Sep 29 '15 at 10:47
  • 2
    What's wrong with two lines? If you don't mind getting a logical vector you can write: `e=((1:n)'==k)`. Or if you do mind: `e=double((1:n)'==k)` – Jens Boldsen Sep 29 '15 at 10:48
  • @JensBoldsen you can just add `0` or else cast directly and still keep that as one line but of doubles instead of logicals – Dan Sep 29 '15 at 10:48
  • I do not see a reason why this would not be elegant enough. – patrik Sep 29 '15 at 10:51
  • Well, all the solutions for this problem are already detailed in the 2nd most frequent `Matlab` question : [How can I index a MATLAB array returned by a function without first assigning it to a local variable?](http://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it). (This question is almost a duplicate actually). None of the solution is really "elegant", but you have quite a choice of options nonetheless. – Hoki Sep 29 '15 at 10:55
  • 1
    @Hoki this question is about a specific operation and is not a duplicate at all in my opinion. The question asks for one line alternatives to a specific case, and three have already been proposed that would not apply to that general question linked to. – Dan Sep 29 '15 at 11:11
  • @Dan, I agree it is not enough a duplicate to be flagged as such, specially when new valid answers are already here. However, the link has some merit as this is somewhat just a "special case" of the more general question in the link (retrieve a subset of a returned variable). And all answers in the link will work for this question. – Hoki Sep 29 '15 at 11:23
  • 2
    If you insist on having a one-liner, you could explicitly define `e = [zeros(1,k-1) 1 zeros(1,n-k)];`. But Dan's answer below is much more transparent, and possibly even faster (not that efficiency is really a problem here). – Andras Deak -- Слава Україні Sep 29 '15 at 12:01

2 Answers2

4

As Matlab automatically pads zeros, you can use:

e([n,k])=[0,1]

Note that this line may produce wrong results when e already exists. To deal with the case k=n, e(n)=0 comes first, then is overwritten by e(k)=1

Daniel
  • 36,610
  • 3
  • 36
  • 69
1

I don't know about one line but this is a more (at least memory) efficient method:

e = zeros(n,1);
e(k) = 1;

Also if you're after elegance and readability then consider just encapsulating this into a function:

function e = basis_vector(n,k)
    e = zeros(n,1);
    e(k) = 1;
end
Dan
  • 45,079
  • 17
  • 88
  • 157