MATLAB Answers

mean of columns in a cell

9 views (last 30 days)
Franziska Domeier
Franziska Domeier on 17 Jan 2020
Commented: Adam Danz on 17 Jan 2020
I have a cell, wich looks like
'S001' [] ' M' 29 '70' '180' '118' '70'
'S002' [] 'F' 25 '58' '166' '130' '90'
'S003' [] 'M' 34 '62' '167' '120' '78'
'S004' [] 'F' 28 '72' '160' '118' '78'
...
Now I would like to have the means of each row. I try
cellfun(@mean,cell_formatiert(3:14,k))
I get an answer, but it is wrong.

  5 Comments

Show 2 older comments
Franziska Domeier
Franziska Domeier on 17 Jan 2020
If I do it with an index, the answer is
Conversion to cell from double is not possible.
If I do it for the columns I need, it is for the fourth:
ans =
29
25
34
28
30
34
24
27
29
27
33
38
and then I can make the mean of it. This works, bit wirth column 5 the answer is:
ans =
51.5000
54.5000
52.0000
52.5000
52.0000
54.0000
54.5000
51.0000
55.0000
53.5000
54.5000
53.0000
and this doesn't make sense
Adam Danz
Adam Danz on 17 Jan 2020
I'm confused. The title of the question states that you want the mean of columns. But then in your question, " I would like to have the means of each row". So, it is rows or columns?
Franziska Domeier
Franziska Domeier on 17 Jan 2020
sorry, I mean column

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 17 Jan 2020
Edited: Adam Danz on 17 Jan 2020
Inputs:
  • cell_formatiert: your mxn cell array (presumably containing a mixture of numeric and character values, otherwise there is a much simpler answer).
  • rows : a vector of row numbers to include in the analysis
  • cols : a vector of column numbers to include in the analysis
cell_formatiert= {
'S001' [] 'M' 29 '70' '180' '118' '70'
'S002' [] 'F' 25 '58' '166' '130' '90'
'S003' [] 'M' 34 '62' '167' '120' '78'
'S004' [] 'F' 28 '72' '160' '118' '78'};
% Select the columns to average for each row
cols = 4:8;
% Select the rows that should be analyzed
rows = 1:4;
Here we convert any character arrays that are within the cols and rows selection to numeric
% identify elements of the selected columns that are character arrays
charIdx = cellfun(@ischar,cell_formatiert);
charIdx(:,~ismember(1:size(cell_formatiert,2),cols)) = false;
% Convert char arrays from chosen columns to numeric
cell_formatiert(charIdx) = num2cell(str2double(cell_formatiert(charIdx)));
If you want to comput the mean of each selected row, given the selected columns
% compute means per selected rows
mu = arrayfun(@(r)mean([cell_formatiert{r,cols}]),rows).';
mu is a vector of row-means, the same size as rows.
If you want to comput the mean of each selected column, given the selected rows
% compute means per selected columns
mu = arrayfun(@(c)mean([cell_formatiert{rows,c}]),cols);
mu is a vector of column-means, the same size as cols.

  4 Comments

Show 1 older comment
Adam Danz
Adam Danz on 17 Jan 2020
Check out the two bold lines of text in my answer. You'll want to use the last line.
Franziska Domeier
Franziska Domeier on 17 Jan 2020
thanks a lot, it works !!!
Adam Danz
Adam Danz on 17 Jan 2020
Glad I could help!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2019a