How do I average columns in cell array if some cells are empty?
4 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have a cell array called new_mat. I would like to compute the mean of all the values in each column and save the result in a new array called averages. I would then have a numerical array with one row and five columns, so five values in total.
One of my columns however contains an empty cell which I think is causing an error.
I have tried this:
averages = cellfun(@(x) mean(x, 1), new_mat);
But I get this error:
Error using cellfun
Non-scalar in Uniform output, at index 24, output 1.
Set 'UniformOutput' to false.
What am I doing wrong?
0 件のコメント
回答 (2 件)
Arif Hoq
2022 年 12 月 12 日
you are asking the same question several times. your cell array "new_mat". giving an answer to your previous question:
a=load("split_newdata_mean.mat");
b=a.split_newdata_mean;
b{1,4}=[b{1,4};NaN]; % making equal dimension in the 4th column
c=[b{:}];
new_mat=c';
averages=mean(new_mat)
1 件のコメント
Arif Hoq
2022 年 12 月 12 日
編集済み: Arif Hoq
2022 年 12 月 12 日
the average of the 5th column is NaN. If you want it as a numerical then add 0 or any value in the 4th column of the cell array.
a=load("split_newdata_mean.mat");
b=a.split_newdata_mean;
b{1,4}=[b{1,4};0]; % making equal dimension in the 4th column
c=[b{:}];
new_mat=c';
averages=mean(new_mat)
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!