필터 지우기
필터 지우기

Creating dummy variables from categorical variable

조회 수: 3 (최근 30일)
Snoopy
Snoopy 2017년 9월 23일
답변: Snoopy 2017년 9월 24일
Suppose there is a column vector array n containing unique but repeating values of the form
1
1
5
7
7
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 0
1 0 0
0 1 0
0 0 1
0 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 0
NaN NaN 0
NaN NaN 0
NaN NaN 1
NaN NaN 1
Where is my mistake in the loop?

채택된 답변

Walter Roberson
Walter Roberson 2017년 9월 23일
for i = 1 : N_ind

추가 답변 (3개)

Stephen23
Stephen23 2017년 9월 24일
Vectorized:
>> vec = [1,1,5,7,7];
>> [~,~,idc] = unique(vec);
>> siz = [numel(idc),max(idc)];
>> out = zeros(siz);
>> out(sub2ind(siz,1:siz(1),idc)) = 1
out =
1 0 0
1 0 0
0 1 0
0 0 1
0 0 1
>>

Guillaume
Guillaume 2017년 9월 24일
Another shorter vectorised option (possibly no faster than Stephen's, I haven't tested):
[~, ~, col] = unique(vec);
out = accumarray([find(col), col], 1) %only purpose of the find is to generate (1:numel(col))'

Snoopy
Snoopy 2017년 9월 24일
I would like to ask a question here. One should always vectorise the code where possible, as I read in the literature. But in this simple example, it seems the for loop is a little less complicated when compared to the vectorised code examples. Would you agree? Or would you think that this depends on how well one knows about MATLAB built-in functions such as accumarray, find, etc.?

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by