How to reshape data?

2 次查看(过去 30 天)
Muhammad shahid
Muhammad shahid 2017-11-7
评论: KL 2017-11-7
I have 30 years daily temperature data in one column. I want to reshape it as monthly average and in 12 columns. The sample for given form and required form is attached.

采纳的回答

KL
KL 2017-11-7
编辑:KL 2017-11-7
If you're using 2016b or later, please use a timetable. Then you can simply call retime to make monthly averages,
TT_monthly = retime(your_TimeTable,'monthly','mean')
With your data, first use readtable to read the data,
data = readtable('A.xls');
then use table2timetable,
TT = table2timetable(data,'RowTimes', data.Date);
then use retime as I have shown before,
TT_monthly = retime(TT,'monthly','mean');
and the result is like,
TT_monthly =
300×2 timetable
Time Date Temperature
__________ __________ ___________
01.01.1986 16.01.1986 18.742
01.02.1986 14.02.1986 19.218
01.03.1986 16.03.1986 22.106
01.04.1986 15.04.1986 29.843
01.05.1986 16.05.1986 32.171
01.06.1986 15.06.1986 37.65 %and so on
  2 个评论
Muhammad shahid
Muhammad shahid 2017-11-7
编辑:Muhammad shahid 2017-11-7
I am using Matlab2012a and not much familiar with mat lab.
KL
KL 2017-11-7
That's a pity. 2012b doesn't even have tables. Anyway a workaround for you is,
data = xlsread('A.xls');
dtvec = datevec(datetime('01.01.1986'):days(1):datetime('31.12.2010'));
C = [dtvec(:,1:3), data]; %create an array [year, month, day, temperature]
y = unique(dtvec(:,1)); %years
monthly_mean = [y zeros(size(y,1),12)]; %years are rows, months are columns
for k = 1:numel(y)
monthly_mean(k,2:end) = accumarray(C(C(:,1)==y(k),2), C(C(:,1)==y(k),4),[],@mean)';
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 矩阵和数组 的更多信息

Community Treasure Hunt

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

Start Hunting!