1

My question is: is there a way to vectorize this operation in MATLAB/Octave?

y = %a (m x 1) vector, with every entry in [1, 10]
y2 = repmat(1 : 10, [m 1]);
for i = 1 : m
    y2(i, :) = (y2(i, :) == y(i));
end
2
  • @macduff: I don't seem to get the correct result with kron. Commented Sep 25, 2012 at 15:06
  • @Jonas, thanks for questioning me, I got a little hasty with my comment. :-) Commented Sep 25, 2012 at 15:26

1 Answer 1

4

bsxfun is a good way of both expanding and vectorizing computations (it will perform a multithreaded computation if it's beneficial).

m = 10;

y = randperm(m);

y2 = bsxfun(@eq,y,(1:m)')';
Sign up to request clarification or add additional context in comments.

1 Comment

@JoseRamirez: Please consider accepting the answer if you consider it helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.