Repeat a cumulative sum in a matrix
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
% 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)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
