Matlab - How to create logical array matrix without looping -


this question has answer here:

i want following:

y = [1; 2; 3]; x = repmat(1:10, 3, 1); i=1:3     x(i,:) = x(i,:) == y(i); end 

so end

x =       1     0     0     0     0     0     0     0     0     0      0     1     0     0     0     0     0     0     0     0      0     0     1     0     0     0     0     0     0     0 

is there way without looping?

if start 1:10 vector, using bsxfun:

y = [1; 2; 3]; x = bsxfun(@eq, y, 1:10); 

otherwise repmat:

y = [1; 2; 3]; x = repmat(1:10, 3, 1); x = repmat(y, 1, size(x,2)) == x; 

(or ones suggested leo.)


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -