reshape and sum multi-dimensional matrix

3 次查看(过去 30 天)
Hi, I have a 20-by-30-by-40 matrix. I would like to sum each two page of the third dimension. In the end, I need to have a 20-by-30-by-20 matrix.
I appreciate if you could help me.

采纳的回答

Stephen23
Stephen23 2018-5-18
编辑:Stephen23 2018-5-18
Where A is your array:
A(:,:,1:2:end) + A(:,:,2:2:end)

更多回答(2 个)

Sammit Jain
Sammit Jain 2018-5-18
Hi, I think what you're trying to do can be accomplished without reshaping the multi-dimensional matrix.
% Initializing 20x30x40 matrix of random elements
X = rand(20,30,40);
% Splitting the matrix into 2 sets based on the alternating third dimension
% The two sets are of dimensions 20x30x20 each.
% Taking out odd elements of third dimension
setA = X(:,:,1:2:40);
% Taking out even elements of third dimension
setB = X(:,:,2:2:40)
% Adding the two sets A and B
result = setA+setB;
The key here is splitting the main matrix into two 'pages' that you actually want to add.
  1 个评论
ehsan
ehsan 2018-5-18
编辑:ehsan 2018-6-28
Thanks for your explanation Sammit. Actually, both answers are correct.

请先登录,再进行评论。


Jan
Jan 2018-6-28
编辑:Jan 2018-6-28
Or:
squeeze(sum(reshape(A, 20, 30, 2, 20), 3))

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by