Creating Dummy Variable without dummyvar

3 Ansichten (letzte 30 Tage)
JoV
JoV am 23 Apr. 2022
Kommentiert: JoV am 23 Apr. 2022
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)

Akzeptierte Antwort

Matt J
Matt J am 23 Apr. 2022
Bearbeitet: Matt J am 23 Apr. 2022
function res = create_Dummies(x)
%create dummy variables for a given vector of values
res=x(:)==unique(x)';
end

Weitere Antworten (0)

Kategorien

Mehr zu Analysis of Variance and Covariance finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by