Average over a cycle

I have a data set of sales at different dates in MATLAB.
I want to find the average sales on day of the cycle, day two of the cyle etc.. (cycle is one week so seven days) of each product.
My data set is csv file with the date in yyyy-mm-dd format and sales in numerical.
To plot my data I have:
f = fopen('salesdata.csv');
data = textscan(f, '%s %f %f %f %f %f ', 'Delimiter', ',', 'HeaderLines', 1);
fclose(f);
data{1} = datenum(data{1});
Date = data{1};
Product3 = data{4};
Product4 = data{5};
Product5 = data{6};
plot(Date, Product 4, 'k')
Please can someone give me any advice!

1 个评论

Five or seven (or other) day week and do you have complete weeks' worth of data or missing or incomplete weeks?
There's a simple way if complete; simply reshape by number of days and average over the N columns.

请先登录,再进行评论。

回答(1 个)

First, do not name your products like this. Use the cell array directly with indexing, or convert to a struct.
A solution to get the averages per week day. First retrieve, the days in the cycle using unique, and then apply accumarray
Days = weekday(data{1}) % some conversion from dates to weekdays
[UniqueDays,~,idx] = unique(Days)
% convert to a struct array for coding convenience
S(1).values = Data{3} ;
S(1).name = 'Product X' ;
% ...
for k = 1:numel(S)
S(k).CycleAvg = accumarray(idx, S(k).values,[],@mean,NaN) ;
end

1 个评论

untested btw, but you should be able to get the point.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

标签

提问:

2016-5-16

Community Treasure Hunt

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

Start Hunting!

Translated by