How to average monthly data using a specific method in Matlab?
1 次查看(过去 30 天)
显示 更早的评论
I have the following vector of monthly values (vectorA). I put the date related info next to it to help illustrate the task but I work with just the vector itself
dates month_in_q vectorA
31/01/2020 1 10
29/02/2020 2 15
31/03/2020 3 6
30/04/2020 1 8
31/05/2020 2 4
30/06/2020 3 3
vectorA = [10;15;6;8;4;3]
How can I create a new vectorNEW according to this algorithm
- In each quarter the first month is the original first month
- In each quarter the second month is the average of first and second month
- In each quarter the third month is the average of all three months
So that I get the following vectorNEW by manipulating the original vectorA in a loop given this the re-occuring pattern above
vectorNEW = [10;12.5;10.3;8;6;5]
dates month_in_q vectorA vectorNEW
31/01/2020 1 10 10
29/02/2020 2 15 AVG(10+15)
31/03/2020 3 6 AVG(10+15+6)
30/04/2020 1 8 8
31/05/2020 2 4 AVG(8+4)
30/06/2020 3 3 AVG(8+4+3)
... ... ... ...
0 个评论
采纳的回答
dpb
2021-5-6
编辑:dpb
2021-5-6
There may be a more clever way, but brute force seems straightforward enough...
A=reshape(vectorA,3,[]); % temporary to get 3-row array
mnV=reshape([A(1,:); mean(A(1:2,:)); mean(A(1:3,:))],[],1); % build desired means, turn to column vector
clear A % remove temporary array
AHA! The "more better" way just dawned...knew had to be a cumsum in here somewhere...
mnV=reshape(cumsum(reshape(vectorA,3,[]))./[1:3].',[],1);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!