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

1 visualización (últimos 30 días)
mark
mark el 12 de Jul. de 2022
Comentada: mark el 12 de Jul. de 2022
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

Respuestas (1)

Jan
Jan el 12 de Jul. de 2022
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 comentario
mark
mark el 12 de Jul. de 2022
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);

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by