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 comentario

dpb
dpb el 16 de Mayo de 2016
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.

Iniciar sesión para comentar.

Respuestas (1)

Jos (10584)
Jos (10584) el 16 de Mayo de 2016

1 voto

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 comentario

Jos (10584)
Jos (10584) el 16 de Mayo de 2016
untested btw, but you should be able to get the point.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 16 de Mayo de 2016

Comentada:

el 16 de Mayo de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by