필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
mark
mark 2022년 7월 12일
댓글: mark 2022년 7월 12일
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