Creating Dummy Variable without dummyvar

조회 수: 3 (최근 30일)
JoV
JoV 2022년 4월 23일
댓글: JoV 2022년 4월 23일
Hello everyone,
I am trying to create a month-level time dummy variable for a "double" variable called lnrent (I am unable to use the dummyvar command due to licensing issues)
I have therefore specified the following function:
function res = create_Dummies(x)
%create dummy variables for a given vector of values
levels = unique(x);
res = [];
for i=levels
res = [res, (x == i)'];
end
end
Where X would be the variable "lnrent" for which the dummy should be created.
However, I keep getting the following error-message when I call it:
Error in create_Dummies (line 10)
res = [res, (x == i)'];
saying that I should preallocate for speed.
How do I do this? I am an absolute beginner and very unsure. Any help would be appreciated!
(I apologise in advance for an community-conduct-errors)

채택된 답변

Matt J
Matt J 2022년 4월 23일
편집: Matt J 2022년 4월 23일
function res = create_Dummies(x)
%create dummy variables for a given vector of values
res=x(:)==unique(x)';
end
  댓글 수: 1
JoV
JoV 2022년 4월 23일
Thank you so much! That worked perfectly

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Analysis of Variance and Covariance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by