Reputation: 1
Hi I'm fairly new to coding (Matlab) and was wondering if anyone knew how to do this. I have a table with numeric data with some non numeric data throughout it. I want to go through the table data and find the average of each row in my table excluding the non numeric figures. Thanks
This is the table:
| a | b | c | d |
|----------|----------|----------|-----------|
| '0.000' | '0.000' | '0.000' | '71.725' |
| '0.000' | '0.000' | 'xxxxx' | '82.778' |
| '0.000' | '0.000' | '0.000' | '84.265' |
| '0.000' | '0.000' | '0.000' | '38.964' |
| '2.258' | '0.000' | '1.628' | '6.646' |
| '0.000' | '0.000' | '1.245' | '129.554' |
| '0.000' | '' | '0.000' | '63.066' |
| '0.000' | '' | '0.303' | '84.729' |
| '0.000' | '0.000' | '-0.001' | '114.079' |
| '0.000' | '0.000' | '0.182' | '20.956' |
| '0.000' | '0.000' | 'xxxxx' | '15.421' |
| '0.000' | '' | 'ygvt' | '57.726' |
| '0.000' | '0.000' | 'ijuhyg' | '85.433' |
| '0.000' | '' | '0.000' | '106.965' |
| '0.000' | '0.000' | '0.294' | '132.627' |
| '0.000' | '0.000' | 'abc' | '46.133' |
| '12.864' | '0.000' | '0.000' | '1.850' |
| '42.991' | 'aaaa' | '14.562' | '47.279' |
| '49.822' | 'bbbb' | '17.713' | '65.436' |
| '0.000' | '0.000' | '0.000' | '85.881' |
| '29.429' | '0.000' | '1.322' | '29.684' |
| '11.784' | 'cccc' | '0.000' | '73.187' |
| '0.000' | '0.000' | '0.000' | '110.376' |
| '0.000' | '0.000' | '0.000' | '66.343' |
| '0.886' | '0.000' | '0.588' | '38.150' |
| '0.000' | '0.000' | '0.000' | '24.694' |
| '23.437' | '22.697' | '0.000' | '9.468' |
| '34.642' | '35.602' | '13.136' | '47.170' |
| '0.000' | '0.000' | '0.000' | '120.737' |
| '0.000' | '0.000' | '0.000' | '196.755' |
| '0.000' | '0.000' | '0.000' | '176.882' |
Upvotes: 0
Views: 59
Reputation: 23858
Looks like all the data in the table is currently strings. Some of those strings look like numbers and can be parsed to them, but they're not actually numeric.
Parse those strings using str2double
to convert them to numerics. The strings that look like numbers will turn in to number values. The strings that don't look like numbers will turn in to NaN
values. Then use nanmean()
to get the average, ignoring NaNs.
>> strs = {'1.234', 'whatever', '23.483', 'banana'}
strs =
1×4 cell array
{'1.234'} {'whatever'} {'23.483'} {'banana'}
>> x = str2double(strs)
x =
1.2340 NaN 23.4830 NaN
>> nanmean(x)
ans =
12.3585
>>
Upvotes: 0