How to collapse within a column vector
Mostra commenti meno recenti
Hello,
I am trying to collapse a long column to a shorter one by suming across elements. For instance, if I have:
x =
1
2
3
4
5
6
7
8
9
I would like to sum up every three elements and arrive at:
y= 6
15
24
This is basically the 'collapse' function in Stata but I'm struggling to do it in Matlab. Thanks for your advide.
Risposta accettata
Più risposte (1)
Alan Stevens
il 17 Lug 2020
One possibility is:
x = 1:12;
for i = 1:length(x)/3
p = 3*(i-1)+1;
y(i) = sum(x(p:p+2));
end
1 Commento
Ara Jo
il 18 Lug 2020
Categorie
Scopri di più su Performance and Memory in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!