cumulative sum of some columns of matrix
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Roisin Loughnane
le 1 Nov 2017
Modifié(e) : Roisin Loughnane
le 1 Nov 2017
I have a 100 x 12 matrix of monthly climate data values.
I want to create a matrix with the cumulative sum of the three previous months.
How can I use cumsum() to get the cumulative sum by row of a certain number of specified columns, not the whole row?
0 commentaires
Réponse acceptée
KL
le 1 Nov 2017
Modifié(e) : KL
le 1 Nov 2017
You seem to want to do cumsum along the second dimension, so trivial is to use
res = cumsum(yourMat,2);
but this will produce the typical cumsum. If you want to add every 3 columns only for a specific row,
yourRow = someval; %within your dimension range
cs = 1:size(yourMat,2);
cs1 = [1 1 cs(1:end-2)];
res = arrayfun(@(x,y) sum(yourMat(yourRow,x:-1:y)), cs, cs1, 'uni',0);
3 commentaires
KL
le 1 Nov 2017
Modifié(e) : KL
le 1 Nov 2017
Ah yeah, I made some changes to my answer. Note that, it's only for a specific row (yourRow). For all the rows, from the top of my head, I'd superimpose it with another arrayfun. It doesn't look very elegant but still works for you.
res = arrayfun(@(z) arrayfun(@(x,y) sum(yourMat(z,x:-1:y)), cs, cs1, 'uni',0),1:yourRow,'uni',0)
Roisin Loughnane
le 1 Nov 2017
Modifié(e) : Roisin Loughnane
le 1 Nov 2017
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!