Question: In Matlab Code: heightconvert Consider a cell of strings containing gender and height information of some patients ( but the data is a little bit

In Matlab Code: heightconvert
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.
>> data={'f''m''f' 'female' 'male'; '5.9''6''172''''180'}; >> heightconvert(data) ans =01.0000001.00001.79831.82881.7200 NaN 1.8000
Pseudocode
You can solve this problem with or without for loops. The following pseudocode describes a solution using for loops:
Initialize m to be a matrix of zeros. Let's process first row of data. For each column of data, Let v be the element on the first row of that column. If v is 'm' or 'male', store a 1 in the corresponding location of m. Now let's process second row of data. For each column of data, Let v be the element on the second row of that column. Use str2double() to convert the string v into a numeric value. If the number is less than 10, convert feet into meters. Otherwise, convert centimeters to meters. Store the value in the corresponding location of m.
In the first for loop, you don't really need to check for 'f' or 'female', because m already contains zeros. You only need to find 'm' or 'male' and change those to 1.
When checking whether a string v is 'm' or 'male', you should not really use the '==' operator. For example, the expression v=='male' will raise an error when v contains 'female', because you'd be comparing vectors of different lengths. Instead of the '==' operator, you need to use the strcmp(a,b) function, which will perform length-checks for you and will return a true only when a and b are of the same lengths and all of their characters are the same. So, strcmp(v,'m') will tell you whether v is exactly 'm'; and strcmp(v,'male') will tell you whether v is exactly 'male'.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!