MATLAB Answers

Error using cellfun Input #2 expected to be a cell array, was double instead.

77 views (last 30 days)
Thar on 2 Oct 2018
Commented: Guillaume on 3 Oct 2018
Dear all,
I want to find mean values of data per day. My data are cells (one cell with 12 columns per day). I did it:
for i=1:size(U(:,1),1);
id{i,1}=[find(data(:,1)==U(i,1) & data(:,2)==U(i,2))];
until z works good and the problem is when calculate the mean values m.
Any ideas???
Thank you.
I took the error: Error using cellfun Input #2 expected to be a cell array, was double instead.


Guillaume on 2 Oct 2018
Z might be a cell array, but it doesn't look like the content of the cells are themselves cell array. While we could suggest ways to correct the problem, it would be much easier if you explained exactly how your data is structured, or even better just attached a mat file. A better explanation for what you're trying to achieve would also be welcome.
In the meantime, note:
  • No need for a semicolon at the end of a for statement. Matlab editor will actually highlight that semicolon and tell you that it is unnecessary. Following the advice of the editor is a good way to avoid bugs.
  • size(U(:, 1), 1) is the same as the simpler size(U, 1).
  • The square brackets [] on your second line are completely unnecessary clutter.
  • The square brackets [] on the cellfun line are also completely unnecessary
  • If id sole purpose is those two lines in the code, then a) there's no need for making it a cell array, and b) the find is completely unnecessary. id could just be a logical array.
  • Similarly, I suspect that Z doesn't need to be indexed. And if data is a matrix, then Z{i, 1}(1:12) is also a matrix, and using cellfun on that is indeed invalid.
I also suspect the loop is unnecessary. But again, better explanation required.
Thar on 2 Oct 2018
My data structured: cells Z (582x1) and each cell (:,1:12) like the attachment file. I would like to find mean value for each cell and for each column of the cell. My final data will be 582 rows and 12 columns.
Thank you

Sign in to comment.

Accepted Answer

Matt J
Matt J on 2 Oct 2018
Edited: Matt J on 2 Oct 2018
m{i,1} = mean(Z{i,1}(1:12));

  1 Comment

Guillaume on 3 Oct 2018
Well that is a bit disappointing, you just accepted this answer which simply modify your very inefficient code whereas I spent a lot of time showing you ways to make your code efficient.

Sign in to comment.

More Answers (1)

Guillaume on 2 Oct 2018
Edited: Guillaume on 2 Oct 2018
I think the explanation that I was asking for is that you have a matrix data whose first column is years and second column days (maybe ?) and you want to average all the other columns by day and year. If so, there are much easier ways to achieve this than what you have done, but first let's start by correcting your code:
z = cell(size(U, 1), 1); %I'm reusing your variable names despite them being very poor names
for i = 1:size(U, 1)
id = data(:, 1) == U(i, 1) & data(:, 2) == U(i, 2);
z{i} = data(id, :);
m = cell2mat(cellfun(@(rows) mean(rows, 1), Z, 'UniformOutput', false))
There are many ways the above could be improved but let's focus on the proper way of doing what you want, which does not involve loops. First option is to use findgroups and splitapply:
id = findgroups(data(:, 1), data(:, 2));
m = splitapply(@(rows) mean(rows, 1), data, id)
That's all!
The second option would be to convert your matrix into a table which has the added benefit of labelling the columns:
t = array2table(data, 'VariableNames', {'year', 'day', 'var3', 'var4', 'var5', 'var6', 'var7', 'var8', 'var9', 'var10', 'var11', 'var12'}) %replace by adequate names
The mean can then be calculated using varfun with the GroupingVariables option:
m = varfun(@mean, t, 'GroupingVariables', {'year', 'day'})
Again, very simple.


Sign in to comment.

Sign in to answer this question.