Suppose there is a column vector array n containing unique but repeating values of the form
11577
The aim is to create a matrix D which contains in its columns dummy variables for each unique value in n of the form
1 0 01 0 00 1 00 0 10 0 1
I use the following code:
uniq = unique(n);N_obs = size(n,1);N_ind = size(uniq,1);D = NaN(N_obs,N_ind);D(:,1) = n == uniq(1,1);D(:,2) = n == uniq(2,1);D(:,3) = n == uniq(3,1);
This produces the desired D matrix. However, it is tedious to write the last three lines so I wanted to use a for loop of the form
for i = N_ind D(:,i) = n == uniq(i,1);end
But this gives
NaN NaN 0NaN NaN 0NaN NaN 0NaN NaN 1NaN NaN 1
Where is my mistake in the loop?
Best Answer