# Sum of elements per hour

조회 수: 5 (최근 30일)
pavlos 2017년 5월 17일
댓글: Steven Lord 2020년 12월 6일
Hello,
Please help me with the following:
consider a matrix M=
[
1-Oct-13 0 2
1-Oct-13 0 6
1-Oct-13 1 2
1-Oct-13 1 6
1-Oct-13 1 6
1-Oct-13 2 2
1-Oct-13 2 0
1-Oct-13 3 2
2-Oct-13 0 1
2-Oct-13 0 1
2-Oct-13 0 3
2-Oct-13 1 4 ...
];
The first column refers to the dates. The second column refers to the hours. The third column refers to some values that are measured per hour.
Note that the matrix have multiple entries per hour and the number of data entries per hour differs.
I need to find the sum per hour of the third column.
For example, for the first day hour=1 has 3 values, hour=2 has 2 values, etc.
The final matrix should have the following form:
N=[ 1-Oct-13 0 8
1-Oct-13 1 14
1-Oct-13 2 2
1-Oct-13 3 2
...
1-Oct-13 24 ...
2-Oct-13 0 3
2-Oct-13 1 4
... ];
If N cannot contains the dates that`s ok, I need only the sums per hour.
Thank you very much.
Best,
Pavlos
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Rik 2017년 5월 17일
You can use calls to unique and find to fill a loop, but I imagine there are better solutions. You could also try to use diff to find out which rows are from the same date and hour.
Just some thoughts. Good luck.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Steven Lord 2017년 5월 17일
If you're stored this data in a timetable, you can use the retime function to generate hourly sums. See the "Aggregate Timetable Data and Calculate Mean Values" example on the retime documentation page and replace 'mean' with 'sum'.
##### 댓글 수: 2없음 표시없음 숨기기
Nathaniel Parker 2020년 12월 6일
This is not working when 'sum' is used. could you help?
Steven Lord 2020년 12월 6일
Please post a small sample of the code that is "not working", describe what "not working" means, and show us at least a few of the rows of the timetable on which you're operating. The head function may help wth that last part.
Does "not working" mean:
• Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
• Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
• Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support using the Contact Support link on the Support section of the MathWorks website so we can investigate.

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Santhana Raj 2017년 5월 17일
try this:
sum(M(find(M(:,1)=='1-Oct-13' & M(:,2)==1),3))
Repeat this in a for loop, and you can generate N.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by