creating dummy variables in a dataset
1 view (last 30 days)
Show older comments
Hi
I'm very new to Matlab...so thanks for your patience. I'm working with the hospital dataset that comes with Matlab (load hospital in Matlab). I want to create a new vector, say DummyGender by looking at the vector hospital.Sex. The rule is:
for x = 1:100;
if hospital.Sex=='Male';
gender{x}=1;
else
gender{x}=-1;
end
end
this works, but I need to go ahead and transpose it, and then convert it to a numerical variable and then finally add it as another column in the dataset. Is there a more efficient way to do the same?
thanks C
2 Comments
Accepted Answer
Walter Roberson
on 21 Mar 2014
transpose() is one way of writing the transpose operation.
It appears you are already creating a numeric value. 1 and -1 are numeric. If you mean that the cell array is to be converted to a numeric array, why not store it as a numeric array in the first place?
Efficient way? I would suggest
gender = zeros(100,1);
for x = 1 : 100
if strcmp( hospital(x).Sex, 'Male')
gender(x) = 1;
else
gender(x) = -1;
end
end
after which no transpose operation is needed as it will already be a column vector.
Even more efficient would be just
Male_tf = strcmp( {hospital.Sex}, 'Male');
gender = 2 * Male_tf(:) - 1;
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!