How can I solve this problem?

7 views (last 30 days)
S
S on 9 Nov 2014
Answered: Geoff Hayes on 10 Nov 2014
Consider a cell of strings containing gender and height information of some patients (but the data is a little bit messy). Gender is given as 'f','female','m' or 'male' and heights are given in feet or centimeter (some data point are missing). For example
{'f' 'm' 'f' 'female' 'male';
'5.9' '6' '172' '' '180' };
Write a function m=heightconvert(data) to convert the cell array data to a matrix of double m. Use 0 to represent female, 1 to represent male. And convert height into meters and represent any missing data points as NaN. Assume that any height value that is less than 10 is in feet and you need the convert those values to meters using 1 foot = 0.3048 meter. Use str2double() function to convert a string to a number.
For example,
>> data={'f' 'm' 'f' 'female' 'male';
'5.9' '6' '172' '' '180' };
>> heightconvert(data)
ans =
0 1.0000 0 0 1.0000
1.7983 1.8288 1.7200 NaN 1.8000
This is what I have so far:
function [ v ] = heightconvert( data )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
[~,n]=size(data);
v=zeros(2,n);
for k=data(:,1:end);
v=data(1,1:end);
if strcmp(v,'m') | strcmp(v,'male')
strcmp(v,'m')== v(1,1:end) | strcmp(v,'male')== v(1,1:end);
end
end
for j=data(:,1:end)
v=data(2,1:end);
w=str2double(v);
if w<10
x=w*0.3048;
x=v(2,1:end);
else w>10
x=w*0.01;
x=v(2,1:end);
end
end
end
This does not give me the correct numbers back. So what am I doing wrong?

Answers (1)

Geoff Hayes
Geoff Hayes on 10 Nov 2014
I think that you have the right idea, it is just that your code can probably be simplified in order to make it work the way that you want. You've rightly started off by pre-sizing your output matrix v to be a fixed size given the input cell array, you just want to make sure that you don't overwrite this which is what your code is currently doing. So let's start with that:
function [ v ] = heightconvert( data )
[~,n]=size(data);
v=zeros(2,n);
In the above, we've assumed that data is a cell array of two rows only, and n columns. What we want to do, is to iterate over each column and determine the sex (0 or 1) and convert the height from feet to metres. We can do this as
for k=1:n
% get the sex
sex = data{1,k};
% if male, set the value in v to one
if strcmpi(sex,'m') || strcmpi(sex,'male')
v(1,k) = 1;
end
% get the height as a number
height = str2double(data{2,k});
% convert the height to metres if not NaN
if ~isnan(height)
if height<10
% height is in inches
height = height * 0.3048;
else
% height is in centimetres
height = height / 100;
end
end
% update v
v(2,k) = height;
end
In the above we use strcmpi to ignore case (lower or upper) and compare the sex with just 'm' or 'male'. str2double is used to convert the height strings to numbers. If one of these heights is invalid or blank, then the conversion sets the height variable (automatically) to NaN.
Try the above and see what happens!

Categories

Find more on Dates and Time 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!