Search code examples
matlabcell-array

Find max number in cell array of Strings (MATLAB)


If I have a cell array M that consist of strings (some of them are strings contain words, other contain numbers)

M={'r' '2'  '17'
   '2' 's'  '15'
   '5' '10' 'rr'
   }

How can I get the maximum number in second column, so the answer will be 10 ?


Solution

  • Use the fact that str2double converts non-numeric strings to NaN, which max() ignores by default:

    mx = max( str2double(M(:,2)) )
    

    To compute the mean, MATLAB's default behavior is to include NaN, so you'll have to tell it to behave otherwise:

    mn = mean( str2double(M(:,2)), 'omitnan')