MATLAB: Sum of each row

matrix

Hi,
I have a set
A=ones(2,3,4);
Here,
val(:,:,1) =
1 1 1
1 1 1
val(:,:,2) =
1 1 1
1 1 1
….
I want to create a matrix whose elements are the sum of each row of A.
The sum of the first row of val(:,:,1) should be a11 in A.
The sum of the second row of val(:,:,1) should be a12 in A.
The sum of the first row of val(:,:,2) should be a21 in A.
The sum of the second row of val(:,:,2) should be a22 in A….
How can i form this 4×2 matrix ?
Thanks in advance.

Best Answer

A = reshape(1:24,2,3,4); % input: [n x m x k] array
val = squeeze(sum(A,2)).'; % output: [k x n] matrix