选择时间序列的一部分 [英] Select portion of timeseries

查看:270
本文介绍了选择时间序列的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我创建了一个简单的数据序列,一个时间向量以及一个时间序列.我没问题画他们两个. (它们在同一图块上并不重要.)我无法弄清楚的是如何对时间序列的一部分进行子集化,如最后一条因错误而失败的命令所示:

In the code below I create a simple data series, a time vector, and then a timeseries. I have no problem plotting both of them. (It's not important that they are on the same plot.) What I haven't been able to figure out is how to subset a portion of a timeseries as shown by the last command that fails with the error:

>> timeseriesTest
Error using timeseries/plot (line 27)
The plot method can only be used for a single timeseries object

Error in timeseriesTest (line 14)
plot(ts(25:end));

>> 

如何提取时间序列中的最后25个值(在这种情况下)?重要说明:虽然在我的时间序列下面的代码中不是正确的,但它具有每日或每周的时间戳记,我确实需要保留该时间戳记.即:

How do I extract the last 25 (in this case) values in the time series? IMPORTANT: While not true in the code below my timeseries have daily or weekly time stamps and I do need to preserve that. I.e.:

NewData = ts.data 

...除非是获取新的提取时间序列的唯一方法,否则它不是一个好答案.

...isn't a good answer unless it's the only way to get a new extracted timeseries.

t=[1:50];
d=sin(2*pi*t/20);

ts = timeseries(2*d, t);

%plot data and timeseries
plot(d);
hold on;
plot(ts);

figure();
plot(d(25:end));
hold on;
plot(ts(25:end));

推荐答案

如果您查看TimeSeries对象的属性,则在尝试绘制图形之前运行代码时,将看到以下内容:

If you look at the properties of a TimeSeries object, when you run your code before you try and plot, this is what we see:

>> ts

  timeseries

  Common Properties:
            Name: 'unnamed'
            Time: [50x1 double]
        TimeInfo: [1x1 tsdata.timemetadata]
            Data: [1x1x50 double]
        DataInfo: [1x1 tsdata.datametadata]

您会看到时间序列对象中有一个Data字段,以及一个表示每个点实例上的时间值的Time字段.如果要单独访问这些字段并绘制最后25个元素,请执行以下操作:

You see that there is a Data field in your time series object, as well as a Time field that represents the time value at each point instance. If you want to individually access the fields, and plot the last 25 elements, do something like this:

plot(ts.Time(end-24:end), squeeze(ts.Data(end-24:end)));

ts是您的TimeSeries对象,如果要访问该对象中的字段,请使用点运算符(. ...,并且您已经弄清楚了).使用点运算符后,您只需使用其适当的名称即可访问所需的字段.因此,如果需要时间值,请使用Time,如果要数据,请使用Data.现在,看起来很奇怪的是我使用了squeeze. squeeze 删除单例尺寸.如您所见,Data是一个1 x 1 x 50数组,而实际上它应该只是一个50 x 1数组. squeeze的目的是删除多余的尺寸以获取我们的实际数据.

ts is your TimeSeries object, and if you want to access fields within this said object, use the dot operator (.... and you've already figured that out). Once you use the dot operator, you simply access the field you want by using its appropriate name. Therefore, if you want the time values, use Time and if you want the data, use Data. Now, what may seem strange is that I used squeeze. squeeze removes singleton dimensions. If you can see, Data is a 1 x 1 x 50 array, when it should really be just a 50 x 1 array. The purpose of squeeze is to remove redundant dimensions to get our actual data.

请注意,这仅在时间序列中只有一个信号时才会发生.如果我们有多个信号……例如,如果我们想要三个长度为50的信号,我们将创建一个50 x 3的矩阵,其中每个表示一个信号.看起来像这样:

Note that this only seems to happen if you only have one signal within your time series. Should we have multiple signals... say, if we wanted three signals of length 50, we would create a 50 x 3 matrix where each column denotes a single signal. It would look something like this:

>> t = 1:50;
>> A = rand(50,3);
>> ts = timeseries(A,t)

  timeseries

  Common Properties:
            Name: 'unnamed'
            Time: [50x1 double]
        TimeInfo: [1x1 tsdata.timemetadata]
            Data: [50x3 double]
        DataInfo: [1x1 tsdata.datametadata]


rand 生成一个随机矩阵或值的向量您希望在[0-1]范围内的任何大小.您会看到我们的信号现在是50 x 3.如果要绘制此图,plot可以在每个时间范围内识别多个信号....因此,您可以执行以下操作:


rand generates a random matrix or vector of values of any size you want in the range of [0-1]. You'll see that our signal is now 50 x 3. If you want to plot this, plot recognizes multiple signals per time frame.... so you can just do this:

plot(ts.Time, ts.Data);

这应生成三个迹线的图,每个迹线用不同的颜色划定并在ts.Time指定的相同时间范围内.

This should generate a plot of three traces, each delineated by a different colour and within the same time frame specified by ts.Time.

类似地,如果要绘制每个信号的最后25个点,只需执行以下操作:

Similarly, if you want to plot the last 25 points for each signal, simply do:

plot(ts.Time(end-24:end), ts.Data(end-24:end,:));

此代码将访问Data中每列(即每个信号)的最后 25行,并将它们全部绘制出来.

This code will access the last 25 rows of every column (i.e. every signal) in your Data and plot them all.

这篇关于选择时间序列的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆