Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Y = eye(K)(y, :); replace a "for" loop? Coursera

Tags:

matlab

octave

Working on an assignment from Coursera Machine Learning. I'm curious how this works... From an example, this much simpler code:

% K is the number of classes.
K = num_labels;
Y = eye(K)(y, :);

seems to be a substitute for the following:

I = eye(num_labels);
Y = zeros(m, num_labels);
for i=1:m
  Y(i, :)= I(y(i), :);
end

and I have no idea how. I'm having some difficulty Googling this info as well.

Thanks!

like image 512
njho Avatar asked May 25 '17 03:05

njho


2 Answers

Your variable y in this case must be an m-element vector containing integers in the range of 1 to num_labels. The goal of the code is to create a matrix Y that is m-by-num_labels where each row k will contain all zeros except for a 1 in column y(k).

A way to generate Y is to first create an identity matrix using the function eye. This is a square matrix of all zeroes except for ones along the main diagonal. Row k of the identity matrix will therefore have one non-zero element in column k. We can therefore build matrix Y out of rows indexed from the identity matrix, using y as the row index. We could do this with a for loop (as in your second code sample), but that's not as simple and efficient as using a single indexing operation (as in your first code sample).

Let's look at an example (in MATLAB):

>> num_labels = 5;
>> y = [2 3 3 1 5 4 4 4];  % The columns where the ones will be for each row
>> I = eye(num_labels)

I =

     1     0     0     0     0
     0     1     0     0     0
     0     0     1     0     0
     0     0     0     1     0
     0     0     0     0     1

>> Y = I(y, :)

Y =
                                       % 1 in column ...
     0     1     0     0     0         %    2
     0     0     1     0     0         %    3
     0     0     1     0     0         %    3
     1     0     0     0     0         %    1
     0     0     0     0     1         %    5
     0     0     0     1     0         %    4
     0     0     0     1     0         %    4
     0     0     0     1     0         %    4

NOTE: Octave allows you to index function return arguments without first placing them in a variable, but MATLAB does not (at least, not very easily). Therefore, the syntax:

Y = eye(num_labels)(y, :);

only works in Octave. In MATLAB, you have to do it as in my example above, or use one of the other options here.

like image 141
gnovice Avatar answered Nov 07 '22 16:11

gnovice


The first set of code is Octave, which has some additional indexing functionality that MATLAB does not have. The second set of code is how the operation would be performed in MATLAB.

In both cases Y is a matrix generated by re-arranging the rows of an identity matrix. In both cases it may also be posible to calculate Y = T*y for a suitable linear transformation matrix T.

(The above assumes that y is a vector of integers that are being used as an indexing variables for the rows. If that's not the case then the code most likely throws an error.)

like image 2
Phil Goddard Avatar answered Nov 07 '22 16:11

Phil Goddard