MATLAB Answers

creating dummy variables in a dataset

41 views (last 30 days)
Chet on 21 Mar 2014
Commented: Chet on 26 Dec 2017
I'm very new to 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';
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
Chet on 26 Dec 2017
Hi George - haven't had a chance to look at this in a while. Do take a look at Walter's response below...maybe that would be useful to you.

Sign in to comment.

Accepted Answer

Walter Roberson
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;
gender(x) = -1;
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;
  1 Comment
Chet on 21 Mar 2014
Thanks Walter! On your comment " 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?" - the dataset that Matlab provides (load 'hospital') has the data as a string to begin with...and I'm trying to convert that to numeric. The code you provided did not work...and that's because I provided the wrong information (like I mentioned, I'm new to Matlab). Male and Female in the database are nominal variables. I guess that's why my original code worked (I don't think one can work with Strings using '==", the code I had up there worked because 'Male' and 'Female' are nominal variables, not String variables). So I need to start from scratch again. Sorry for wasting your time, and thanks for your help again. Thanks Chet
BTW - the dataset hospital came preloaded on my version of Matlab

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!