Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
mark
mark am 12 Jul. 2022
Kommentiert: mark am 12 Jul. 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

Antworten (1)

Jan
Jan am 12 Jul. 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 Kommentar
mark
mark am 12 Jul. 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);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by