Cumulative element-wise average from 3D matrix

조회 수: 3 (최근 30일)
J Toole
J Toole 2022년 8월 28일
댓글: J Toole 2022년 8월 28일
My dataset is a 3D matrix (3x5x2). How can I create a new 3x5x2 matrix where the respective elements in the 1st dimension are cumulatively averaged across the 2nd dimension?
I want a new matrix that in which:
(:,1,1) and (:,1,2) remain the same (the average of each 1st element in first dimension is itself).
(:,2,1) and (:,2,2) are the average of the first 2 elements in each respective row (i.e. 2nd dimension).
(:,3,1) and (:,3,2) are the average of the first 3 elements in each respective row, and so on....
For example,
A = rand(3,5,2)
A(:,:,1) =
0.3444 0.9392 0.7279 0.2141 0.7071
0.8340 0.0112 0.3847 0.1898 0.9043
0.6843 0.4466 0.8988 0.2104 0.2948
A(:,:,2) =
0.5756 0.6006 0.4804 0.6754 0.1328
0.5651 0.8882 0.3092 0.5088 0.7747
0.8765 0.9748 0.6653 0.0476 0.1058
With the above A, I want to creat the below (new) matrix of cumulatively averaged respective rows.
A_new(:,:,1) =
0.3444 0.6418 0.6705 0.5564 0.5865
0.8340 0.4226 0.4100 0.3549 0.4648
0.6843 0.5655 0.6766 0.5600 0.5070
A_new(:,:,2) =
0.5756 0.5881 0.5522 0.5830 0.4930
0.5651 0.7267 0.5875 0.5678 0.6092
0.8765 0.9257 0.8389 0.6411 0.5340

채택된 답변

Walter Roberson
Walter Roberson 2022년 8월 28일
A_new = cumsum(A, 2) ./ (1:size(A,2));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by