Please let me know a way to use for loop to extract the first row elements from each column of data and check a condition and store these in a matrix
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
DGM
2021년 11월 26일
Something like this
C = {'f' 'm' 'f' 'female' 'male';
'5.9' '6' '172' '' '180' };
g = double(cellfun(@(x) x(1)=='m',C(1,:)));
h = cellfun(@str2double,C(2,:));
mask = h>=10;
h(mask) = h(mask)/100;
h(~mask) = h(~mask)*0.3048;
% show results
g
h
You'll still have to wrap this up as a function.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!