- 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.
Error using cellfun Input #2 expected to be a cell array, was double instead.
29 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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))];
Z{i,1}=data(id{i,1},1:12);
m{i,1}=cellfun(@mean,[Z{i,1}(1:12)],'un',0);
end
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.
2 Kommentare
Guillaume
am 2 Okt. 2018
Bearbeitet: Guillaume
am 2 Okt. 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:
I also suspect the loop is unnecessary. But again, better explanation required.
Akzeptierte Antwort
Matt J
am 2 Okt. 2018
Bearbeitet: Matt J
am 2 Okt. 2018
m{i,1} = mean(Z{i,1}(1:12));
1 Kommentar
Guillaume
am 3 Okt. 2018
@Thar,
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.
Weitere Antworten (1)
Guillaume
am 2 Okt. 2018
Bearbeitet: Guillaume
am 2 Okt. 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, :);
end
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
m = varfun(@mean, t, 'GroupingVariables', {'year', 'day'})
Again, very simple.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!