matlab - is there a better way of assigning values to a matrix -
say have matrix
a=zeros(10,3);
and vector
ll=[1 1 1 2 2 2 3 1 3 2]';
and want assign value in each row corresponding value in ll row 1
i.e output be
a= 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 1 0 0 0 1 1 0 0 0 0 1 0 1 0
how using loop
for ii=1:length(ll) a(ii,ll(ii)=1; end
this should trick:
ll=[1 1 1 2 2 2 3 1 3 2]'; a=bsxfun(@eq,ll,1:max(ll))
i'm using bsxfun
check when entry of ll
equal element of row vector [1 2 3]
(in case). if entry of ll
1, equal entry in first column of [1 2 3]
vector , give 1 in first column of a
, zeros in rest of columns of row.
matlab
No comments:
Post a Comment