Cumulative element-wise average from 3D matrix

1 vue (au cours des 30 derniers jours)
J Toole
J Toole le 28 Août 2022
Commenté : J Toole le 28 Août 2022
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

Réponse acceptée

Walter Roberson
Walter Roberson le 28 Août 2022
A_new = cumsum(A, 2) ./ (1:size(A,2));
  1 commentaire
J Toole
J Toole le 28 Août 2022
Perfect, thank you very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by