필터 지우기
필터 지우기

reshape and sum multi-dimensional matrix

조회 수: 5 (최근 30일)
ehsan
ehsan 2018년 5월 18일
편집: Jan 2018년 6월 28일
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