Effacer les filtres
Effacer les filtres

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

4 vues (au cours des 30 derniers jours)
mark
mark le 12 Juil 2022
Commenté : mark le 12 Juil 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

Réponses (1)

Jan
Jan le 12 Juil 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 commentaire
mark
mark le 12 Juil 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);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by