Sum of all columns in a 3D cell array

4 views (last 30 days)
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)
  2 Comments
David Hill
David Hill on 9 Jun 2020
I am confused. What is in each cell element? Or do you just have a 301x51x3 matrix in a single cell element?
Chris Dan
Chris Dan on 9 Jun 2020
each cell element has a value of 1, each cell element is not a matrix, it is just one value

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 9 Jun 2020
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

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!