I have table with T
date location sales
--------------------
1/1 NY 100
1/2 NY 105
1/1 LA 70
1/2 LA 90
I want to sum up over dates to create T1 with
1/1 170
1/2 195
What is the next step?

댓글 수: 1

alpedhuez
alpedhuez 2021년 7월 29일
https://www.mathworks.com/matlabcentral/answers/406864-how-do-i-sum-over-one-dimension-of-a-multidimensional-array

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

 채택된 답변

Cris LaPierre
Cris LaPierre 2021년 7월 29일
편집: Cris LaPierre 2021년 7월 29일

0 개 추천

Convert your dates to datetimes and use groupsummary with the groupbins input set to 'day' and your method set to 'sum', and datavars specified as your sales variable. Use the following syntax.

댓글 수: 13

alpedhuez
alpedhuez 2021년 7월 29일
what for groupvars?
Cris LaPierre
Cris LaPierre 2021년 7월 29일
What do you want to group by?
So is it
groupsummary(T,'date','day','sum','sales');
?
Cris LaPierre
Cris LaPierre 2021년 7월 29일
편집: Cris LaPierre 2021년 7월 29일
Looks good to me, but you have the data, so why not run it and see if you get the result you want?
Now suppose I have two variables
date location sales visitors
--------------------
1/1 NY 100 3
1/2 NY 105 5
1/1 LA 70 6
1/2 LA 90 10
I want to sum sales and visitors for each date:
date sales visitors
--------------------
1/1 170 9
1/2 195 15
Does it
groupsummary(T,'date','day','sum',{'sales','visitors'});
?
Cris LaPierre
Cris LaPierre 2021년 7월 29일
What happens when you run that line of code?
alpedhuez
alpedhuez 2021년 7월 29일
Yes it works fine. Now is it possible to "sum" for a variable and "mean" for another variable?
Cris LaPierre
Cris LaPierre 2021년 7월 29일
편집: Cris LaPierre 2021년 7월 29일
Yes. To take the sum and mean of sales and visitors, I'd do this
groupsummary(T,'date','day',{'sum','mean'},{'sales','visitors'});
alpedhuez
alpedhuez 2021년 7월 29일
But then how it goes with the sum for two variable and mean for one vaiable?
Cris LaPierre
Cris LaPierre 2021년 7월 29일
편집: Cris LaPierre 2021년 7월 29일
I misspoke earlier. All methods are applied to all datavars. With groupsummary, it is not possible to separate the stats by variable. To do that, i would probably compute the all stats for all variables, then remove the columns I do not want.
No I mean, suppose I now have three variables sales, visitors, profits and want to take sum of sales and visitors and mean of profits. Is it
groupsummary(T,'date','day',{'sum','sum','mean'},{'sales','visitors','profits'});
?
alpedhuez
alpedhuez 2021년 7월 29일
What will be pseudo code?
Cris LaPierre
Cris LaPierre 2021년 7월 29일
See my previous reply. All methods are applied to all datavars.
All your answers can be found by going through the documentation for groupsummary.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

릴리스

R2020b

태그

질문:

2021년 7월 29일

댓글:

2021년 7월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by