How to reshape data?

5 Ansichten (letzte 30 Tage)
Muhammad shahid
Muhammad shahid am 7 Nov. 2017
Kommentiert: KL am 7 Nov. 2017
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.

Akzeptierte Antwort

KL
KL am 7 Nov. 2017
Bearbeitet: KL am 7 Nov. 2017
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 Kommentare
Muhammad shahid
Muhammad shahid am 7 Nov. 2017
Bearbeitet: Muhammad shahid am 7 Nov. 2017
I am using Matlab2012a and not much familiar with mat lab.
KL
KL am 7 Nov. 2017
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Logical finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!