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

Viewed 3143

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!

2 Answers
Related