Sum of previous elements in a matrix(dynamic way)
Afficher commentaires plus anciens
Hi, let's say I have the following vector matrix: A=[1 2 3; 4 5 6;7 8 9;10 11 12];
I'm looking for a dynamic way to sum the previous elements(Matrix B the results). I would like to have the variable L to do that.
Example 1: L=1 means I won't to sum all element with their previous, wich give the following result:
B=[5 7 9;11 13 15;17 19 21]
Example 2: L=2 means I won't to sum all element with their 2 previous elements, wich give the following result:
B=[12 15 18;21 24 27]
L need to be dynamic.
A concrete example would be appreciate a an awnser.
Thank you in adavance,
Réponse acceptée
Plus de réponses (2)
Azzi Abdelmalek
le 27 Nov 2012
Modifié(e) : Azzi Abdelmalek
le 27 Nov 2012
A=[1 2 3; 4 5 6;7 8 9;10 11 12];
out=A(1:end-1,:)+A(2:end,:)
Walter Roberson
le 27 Nov 2012
Modifié(e) : Walter Roberson
le 27 Nov 2012
T = cumsum(A);
T(L+1:end,:) - T(1:end-L)
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!