Matlab 对于命令+;你需要一些提示吗
我有一个矩阵Matlab 对于命令+;你需要一些提示吗,matlab,datetime,loops,interpolation,Matlab,Datetime,Loops,Interpolation,我有一个矩阵a,它有三列:每日日期、价格和小时数-都是相同大小的向量-一天中有多个与小时数相关的价格 样本数据如下: A_dates = A_hours= A_prices= [20080902 [9.698 [24.09 20080902 9.891 24.59 200080902 10.251 24.60 20080903 9
a
,它有三列:每日日期、价格和小时数-都是相同大小的向量-一天中有多个与小时数相关的价格
样本数据如下:
A_dates = A_hours= A_prices=
[20080902 [9.698 [24.09
20080902 9.891 24.59
200080902 10.251 24.60
20080903 9.584 25.63
200080903 10.45 24.96
200080903 12.12 24.78
200080904 12.95 26.98
20080904 13.569 26.78
20080904] 14.589] 25.41]
- 请记住,我有大约两年的每日数据,每天大约有10000个价格,几乎涵盖每天上午9:30到下午16:00的每分钟。实际上,我的初始数据集时间是以毫秒为单位的。然后我把毫秒换算成小时。我有一些时间,比如14.589,用3种不同的价格重复了三次。因此,我做了以下工作: 时间=[A_日期、A_小时、A_价格]; [timeinhr,price]=合并者(时间,A_价格,'mean');其中timeinhr是向量A_日期和A_小时
%# here I want hours in 0.25unit increments (like 9.5hrs)
new_timeinhr = 0:0.25:max(A_hours));
day_hour = rem(new_timeinhour, 24);
%# Here I want only prices between 9.5hours and 16hours
new_timeinhr( day_hour <= 9.2 | day_hour >= 16.1 ) = [];
我的错误是:
在赋值A(I)=B中,B和I中的元素数必须相同。
如何将
int\u prices(j)
写入堆栈?我想我可以这样解决:
new_timeinhr = 0:0.25:max(A(:,2));
day_hour = rem(new_timeinhr, 24);
new_timeinhr( day_hour <= 9.4 | day_hour >= 16.1 ) = [];
days=unique(data(:,1));
P=[];
for j=1:length(days);
condition=A(:,1)==days(j);
intprices = interp1(A(condition,2), A(condition,3), new_timeinhr);
P=vertcat(P,intprices');
end;
new_timeinhr=0:0.25:max(A(:,2));
日小时=rem(新时间,24);
新时长(日时=16.1)=[];
天数=唯一(数据(:,1));
P=[];
对于j=1:长度(天);
条件=A(:,1)=天(j);
intprices=interp1(条件2),条件3,新时间;
P=垂直猫(P,intprices');
终止
我想我可以这样解决这个问题:
new_timeinhr = 0:0.25:max(A(:,2));
day_hour = rem(new_timeinhr, 24);
new_timeinhr( day_hour <= 9.4 | day_hour >= 16.1 ) = [];
days=unique(data(:,1));
P=[];
for j=1:length(days);
condition=A(:,1)==days(j);
intprices = interp1(A(condition,2), A(condition,3), new_timeinhr);
P=vertcat(P,intprices');
end;
new_timeinhr=0:0.25:max(A(:,2));
日小时=rem(新时间,24);
新时长(日时=16.1)=[];
天数=唯一(数据(:,1));
P=[];
对于j=1:长度(天);
条件=A(:,1)=天(j);
intprices=interp1(条件2),条件3,新时间;
P=垂直猫(P,intprices');
终止
我建议将输入转换为单一单调时间值。使用MATLABdatenum
格式,将一天表示为1。这样做有很多好处:可以获得内置的MATLAB时间/日期函数,可以通过datetick
获得格式良好的日期/时间打印标签,插值也可以正常工作。如果没有测试数据,我无法测试这段代码,但这里是总体思路
根据日期存储为20080902(我假设为yyyymmdd)的新信息,我更新了初始转换代码。另外,由于A的布局会引起混乱,我将把A的列称为向量A\u prices
,A\u hours
,和A\u dates
% This datenum vector matches A. I'm assuming they're already sorted by date and time
At = datenum(num2str(A_dates), 'yyyymmdd') + datenum(0, 0, 0, A_hours, 0, 0);
incr = datenum(0, 0, 0, 0.25, 0, 0); % 0.25 hour
t = (At(1):incr:At(end)).'; % Full timespan of dataset, in 0.25 hour increments
frac_hours = 24*(t - floor(t)); % Fractional hours into the day
t_business_day = t((frac_hours > 9.4) & (frac_hours < 16.1)); % Time vector only where you want it
P = interp1(At, A_prices, t_business_day);
%此datenum向量与A匹配。我假设它们已按日期和时间排序
At=datenum(num2str(A_dates),'yyyyymmdd')+datenum(0,0,0,A_hours,0,0);
incr=datenum(0,0,0,0.25,0,0);%0.25小时
t=(在(1):增量:在(结束));%数据集的完整时间跨度,以0.25小时为增量
压裂时间=24*(t-地面(t));%一天中的零碎时间
t_business_uday=t((frac小时>9.4)和(frac小时<16.1));%时间向量只在你想要的地方
P=interp1(At、A价格、t工作日);
我重复一遍,因为没有测试数据,所以我不能测试代码。我强烈建议测试日期转换代码,使用
datestr
将datenum转换回可读日期。我建议将输入转换为单一单调时间值。使用MATLABdatenum
格式,该格式表示一天为1。这样做有很多好处:可以获得内置的MATLAB时间/日期函数,可以通过datetick
获得格式良好的日期/时间打印标签,插值也可以正常工作。如果没有测试数据,我无法测试这段代码,但这里是总体思路
根据日期存储为20080902(我假设为yyyymmdd)的新信息,我更新了初始转换代码。另外,由于A的布局会引起混乱,我将把A的列称为向量A\u prices
,A\u hours
,和A\u dates
% This datenum vector matches A. I'm assuming they're already sorted by date and time
At = datenum(num2str(A_dates), 'yyyymmdd') + datenum(0, 0, 0, A_hours, 0, 0);
incr = datenum(0, 0, 0, 0.25, 0, 0); % 0.25 hour
t = (At(1):incr:At(end)).'; % Full timespan of dataset, in 0.25 hour increments
frac_hours = 24*(t - floor(t)); % Fractional hours into the day
t_business_day = t((frac_hours > 9.4) & (frac_hours < 16.1)); % Time vector only where you want it
P = interp1(At, A_prices, t_business_day);
%此datenum向量与A匹配。我假设它们已按日期和时间排序
At=datenum(num2str(A_dates),'yyyyymmdd')+datenum(0,0,0,A_hours,0,0);
incr=datenum(0,0,0,0.25,0,0);%0.25小时
t=(在(1):增量:在(结束));%数据集的完整时间跨度,以0.25小时为增量
压裂时间=24*(t-地面(t));%一天中的零碎时间
营业日=营业日(营业时数>9.4)和营业时数<16.1%)%时间向量只在你想要的地方
P=interp1(At、A价格、t工作日);
我重复一遍,因为没有测试数据,所以我不能测试代码。我强烈建议测试日期转换代码,使用
datestr
将日期从datenum转换回可读日期。按照建议将天/小时转换为序列日期编号绝对是一种方法。基于他的代码(我已经投了赞成票),我在下面给出一个简单的例子
首先,我先创建一些类似于您描述的虚假数据(还有一些缺失的部分):
%#三天,以1小时为增量
dt=datenum(num2str((0:23)”,'2012-06-01%02d:00'),'yyyy-mm-dd-HH:mm');%
dt=[dt;dt+1;dt+2];
%#每小时对应的价格数据
p=总和(兰特(尺寸(dt))-0.5);
%#显示图
绘图(dt,p,'.-',日期标记('x'))
网格打开,xlabel('日期/时间'),ylabel('价格')
%#让我们删除一些丢失的行
idx=(兰特(大小(dt))<0.1);
暂停,绘图(dt(idx),p(idx),‘ro’),暂停
图例({'prices','missing'})
dt(idx)=[];
p(idx)=[];
%#矩阵与您的相同:天数、价格、时间
ymd=str2double(cellstr(datestr(dt,'yyyyymmdd'));
hr=str2double(cellstr(datestr(dt,'HH'));
A=[ymd p hr];
%#清除除数据矩阵A之外的所有变量
clearvars-除A之外
下一个
%# convert days/hours to serial date number
dt = datenum(num2str(A(:,[1 3]),'%d %d'), 'yyyymmdd HH');
%# create a vector of 15 min increments
t_15min = (0:0.25:(24-0.25))'; %#'
tt = datenum(0,0,0, t_15min,0,0);
%# offset serial date across all days
ymd = datenum(num2str(unique(A(:,1))), 'yyyymmdd');
tt = bsxfun(@plus, ymd', tt); %#'
tt = tt(:);
%# interpolate data at new datetimes
pp = interp1(dt, A(:,2), tt);
%# extract desired period of time from each day
idx = (9.5 <= t_15min & t_15min <= 16);
idx2 = bsxfun(@plus, find(idx), (0:numel(ymd)-1)*numel(t_15min));
P = pp(idx2(:));
%# plot interpolated data, and show extracted periods
figure, plot(tt, pp, '.-'), datetick('x'), hold on
plot([tt(idx2);nan(1,numel(ymd))], [pp(idx2);nan(1,numel(ymd))], 'r.-')
hold off, grid on, xlabel('Date/Time'), ylabel('Prices')
legend({'interpolated prices','period of 9:30 - 16:00'})