calculate mean from cell array with mixed numbers and strings

user6110593

I have a cell array containing numbers and strings. Can someone help me with a code that can calculate the mean of the numbers in each row?

Mix = {

      'U'             [4.7506e+05]    [5.0141e+05]    [5.4067e+05]    [    542976]    [5.7124e+05]
      [3.9810e+05]    [4.4926e+05]    'U'             [    531440]    [5.3655e+05]    'O' 
      [3.0915e+05]    [3.6336e+05]    [3.7107e+05]    [3.8252e+05]    [3.3988e+05]    [4.3384e+05]
      'U'             'U'             'U'             'O'             'U'             [3.7107e+05]         
      'U'             [3.7107e+05]    'U'             [3.8252e+05]    'U'             'O'     }

I did solve it with some help from a previous question: Find mean of an array with both numbers and strings with Matlab

Solution:

for k = 1:num
    isNum = cellfun(@isnumeric, Mix(k,:)); % find number in the row
    Y(k,1) = mean(cell2mat(Mix(k,isNum))); % convert to mat for mean
end
Luis Mendo

Here's a way:

Y = mean(str2double(cellfun(@num2str, Mix, 'UniformOutput', false)), 2, 'omitnan');

This works as follows:

  1. cellfun(@num2str, Mix, 'UniformOutput', false) converts each cell to a string.
  2. str2double(...) converts each cell into a number. This gives NaN for cells that didn't originally contain a number. The result is a standard, numeric array.
  3. mean(..., 2, 'omitnan') computes the mean of each row ignoring NaN values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

how to convert string values to numbers in a mixed array

From Dev

Calculate mean cell values over different files

From Dev

Mysql Sorting mixed strings of letters and numbers, letters before numbers

From Dev

Extract Numbers from Array mixed with strings - Javascript

From Dev

Removing 'NaN' strings and [] cells from cell array in Matlab

From Dev

How to calculate mean of columns from array lists in python using numpy?

From Dev

Export from Matlab to Stata an array of numbers and strings

From Dev

Excel: calculate the amount - the numbers from a cell (58391) in another cell (5+8+3+9+1)

From Dev

Calculate the mean of an array in MATLAB

From Dev

Calculate the mean of an array of n numbers in C

From Dev

ReactJS: Calculate average from array of strings in JSON object during render

From Dev

Find strings of a cell array in another cell array

From Dev

Excel: extracting a number from mixed content cell

From Dev

python max function with mixed strings and numbers

From Dev

Calculate the SUM() time from columns with mixed types

From Dev

Convert cell array to array of strings

From Dev

calculate daily mean of an array in python

From Dev

Create matrices from strings in a cell-array

From Dev

Calculate at least 3 minimum and maximum numbers from array

From Dev

Calculate mean of array with specific value from another array

From Dev

numbers and strings in array python

From Dev

Calculate the mean values if the numbers are same

From Dev

Removing duplicate strings from a nested cell array

From Dev

Calculate squared deviation from the mean for each element in array

From Dev

How to extract numbers from mixed strings

From Dev

TypeScript creating Set/Array from array of numbers or strings

From Dev

PureScript - Calculate the mean of an array of numbers

From Dev

Calculate mean based on range within a cell

From Dev

how to get numbers from array of strings?

Related Related

  1. 1

    how to convert string values to numbers in a mixed array

  2. 2

    Calculate mean cell values over different files

  3. 3

    Mysql Sorting mixed strings of letters and numbers, letters before numbers

  4. 4

    Extract Numbers from Array mixed with strings - Javascript

  5. 5

    Removing 'NaN' strings and [] cells from cell array in Matlab

  6. 6

    How to calculate mean of columns from array lists in python using numpy?

  7. 7

    Export from Matlab to Stata an array of numbers and strings

  8. 8

    Excel: calculate the amount - the numbers from a cell (58391) in another cell (5+8+3+9+1)

  9. 9

    Calculate the mean of an array in MATLAB

  10. 10

    Calculate the mean of an array of n numbers in C

  11. 11

    ReactJS: Calculate average from array of strings in JSON object during render

  12. 12

    Find strings of a cell array in another cell array

  13. 13

    Excel: extracting a number from mixed content cell

  14. 14

    python max function with mixed strings and numbers

  15. 15

    Calculate the SUM() time from columns with mixed types

  16. 16

    Convert cell array to array of strings

  17. 17

    calculate daily mean of an array in python

  18. 18

    Create matrices from strings in a cell-array

  19. 19

    Calculate at least 3 minimum and maximum numbers from array

  20. 20

    Calculate mean of array with specific value from another array

  21. 21

    numbers and strings in array python

  22. 22

    Calculate the mean values if the numbers are same

  23. 23

    Removing duplicate strings from a nested cell array

  24. 24

    Calculate squared deviation from the mean for each element in array

  25. 25

    How to extract numbers from mixed strings

  26. 26

    TypeScript creating Set/Array from array of numbers or strings

  27. 27

    PureScript - Calculate the mean of an array of numbers

  28. 28

    Calculate mean based on range within a cell

  29. 29

    how to get numbers from array of strings?

HotTag

Archive