calculate mean for elements of each column in a cell array sub-entry

4 次查看(过去 30 天)
Hi,
I have a cell array, cell_array, and I am trying to calculate the mean of all the elments in each column of it's subcomponent, cell_array{3,1}:
cell_array_mean_vals=cell(1,6) %cell_array is 4x6 and cell_array_mean_vals is initialised to be 1x6
for i=1:size(cell_array{3,1},2)
cell_array_mean_vals{1, i}=mean2(cell_array{3,1}(:,i));
end
Error: mean2 requries the image processing toolbox and i cant use 'mean' or 'sum' because cell_array{3,1} contains doubles
Thank you

回答(1 个)

Jan
Jan 2022-7-12
Of courser you can use mean and sum for double arrays. Why do you think, that this is not possible?
cell_array{3,1} = magic(4); % Some test data
for i=1:size(cell_array{3,1},2)
cell_array_mean_vals{1, i} = mean(cell_array{3,1}(:,i));
end
cell_array_mean_vals
cell_array_mean_vals = 1×4 cell array
{[8.5000]} {[8.5000]} {[8.5000]} {[8.5000]}
% And without a loop:
result2 = num2cell(mean(cell_array{3,1}, 1))
withoutLoop = 1×4 cell array
{[8.5000]} {[8.5000]} {[8.5000]} {[8.5000]}
  1 个评论
mark
mark 2022-7-12
thank you for your response!
I get this error when I try both approaches:
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in mean
y=sum(x,dim,flag)./mysize(x,dim);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by