MATLAB: Sum of all columns in a 3D cell array

3d matricescell arrayssum

Hello,
I have a cell array by the name phi_z = {301x51x3}
I want to take a sum of all its columns in a single slice, so it would become phi_z = {301x1x3}. I have been reading the documentation
but I could not find it.
after wards, I would convert the cell array to double array.
does anyone know?
phi_z = cell(301,51,3);
out=cell2mat(phi_z)
A = sum(out,2)

Best Answer

Here are the inputs according to your description
pyi_z = num2cell(rand(301,51,3));
% Check size
size(pyi_z)
ans =
301 51 3
% look at first 10 values and check format
pyi_z(1:10)
ans =
1×10 cell array
Columns 1 through 5
{[0.50532]} {[0.7123]} {[0.72638]} {[0.74586]} {[0.62423]}
Columns 6 through 10
{[0.27062]} {[0.80714]} {[0.29222]} {[0.21049]} {[0.60563]}
Convert from cell to numeric array, compute sum of columns
% Convert to numeric array
pyi_zArray = cell2mat(pyi_z);
% Sum of columns
t = sum(pyi_zArray,2);
% Check size of output
size(t)
ans =
301 1 3