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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!