Repeat a cumulative sum in a matrix
Mostrar comentarios más antiguos
Hello,
I have matrix of dimensions 2784x252 which represents monthly returns. I am trying to put them into annual returns which would return a matrix of 2784x21 (21 x 12 = 252). To do that I need to sum for each row, 12 column at a time. I've tried to use the function cumsum but it won't work because it returns an array with only one column which isn't what I need to do.
Since I need to repeat the sum of 12 column 21 times which is the number of years I have, I thought using a loop could help but I can't find a way that it woulld work.
Thanks for the help,
Best regards,
Frank
Respuesta aceptada
Más respuestas (1)
the cyclist
el 16 de Ag. de 2021
Editada: the cyclist
el 16 de Ag. de 2021
% Made-up data
M = rand(2784,252);
% Reshape to put each year in a "slice" in the 3rd dimension
M2 = reshape(M,2784,12,[]);
% Show size of M2, just to illustrate
size(M2)
% Get annual return by summing the monthly returns
% (That's not really accurate, though, right?
% Constant 1% monthly return will compound to greater than 12% annual return.)
A2 = sum(M2,2);
% Show size of A2, just to illustrate
size(A2)
% Reshape again, to get back to a 2D array
A = reshape(A2,2784,[]);
% Show size again
size(A)
1 comentario
Francis Chabot
el 16 de Ag. de 2021
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
