creating dummy variables in a dataset
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Chet
el 21 de Mzo. de 2014
Comentada: Chet
el 26 de Dic. de 2017
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 comentarios
Respuesta aceptada
Walter Roberson
el 21 de Mzo. de 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;
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!