How to reshape data?

4 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!