这是一件小事,但一直困扰我一段时间,所以我想我会让人群解决开始:)
我有一个带时间戳和相应逻辑值(或1/0)的矩阵,即
of = [-inf 0 10 15 190 inf; 1 0 0 1 1 0]'和另一个时间向量,例如
t = 0:0.1:1e3;或者其他什么,你明白了:)
现在,我如何(读取:你会)对逻辑信息进行内部/外推,使其与t中的时间戳匹配,但内插逻辑总是假设最后一个或当前值,而不是未来的值?
不知道这是否有意义,但这里是给定of和t2的预期输出
t2 = [0 5 14 16] output = [0 0 10 15; 0 0 0 1]'其中第一列output是插值of使用的时间。 如果我使用interp1和'最近'算法,它会给出
interp1(of(:,1), of, t2, 'nearest') output = [0 10 15 15; 0 0 1 1]'这不完全是我想要的。
This is a small thing but has been bothering me for a while now, so I thought I would let the crowd solving begin :)
I have a matrix with timestamps and a corresponding logical value (or 1/0), i.e.
of = [-inf 0 10 15 190 inf; 1 0 0 1 1 0]'and an another time vector, e.g.
t = 0:0.1:1e3;or whatever, you get the point :)
Now how do I (read: would you) inter-/extrapolate the logical infomation in of so it matches the timestamps in t, but with the interpolated logicals always assuming the last or current value, not a future one?
Don't know if that makes sense, but here is the expected output given of and t2
t2 = [0 5 14 16] output = [0 0 10 15; 0 0 0 1]'where the first column of output is the time of of used in interpolation. If I use interp1 and the 'nearest' algorithm, it will give
interp1(of(:,1), of, t2, 'nearest') output = [0 10 15 15; 0 0 1 1]'which is not exactly what I want.
最满意答案
假设您的向量已经排序,您可以尝试,这似乎适用于您的示例,虽然我没有广泛测试它:
of=[-inf 0 10 15 190 inf; 1 0 0 1 1 0]'; t2 = [0 5 14 16]; index=floor(interp1(of(:,1),(1:size(of,1))',t2')); output=of(index,:);希望能帮助到你。
interp1使用的默认方法是linear ,这对你的条件最有效,因为你不想要“最近的”邻居,而是第一个较低或相等的邻居(据我所知)。 因此,插值时间戳索引的简单截断将为您提供结果。
Assuming your vectors are sorted you could try that, which seems to work with your example although I did not tested it extensively:
of=[-inf 0 10 15 190 inf; 1 0 0 1 1 0]'; t2 = [0 5 14 16]; index=floor(interp1(of(:,1),(1:size(of,1))',t2')); output=of(index,:);Hope it helps.
The default method used by interp1 is linear, which works best with your condition because you do not want the "nearest" neighbor but the first lower or equal neighbor (as far as I understand this). Therefore a simple truncation of the interpolated timestamps index gives you the result.
更多推荐
发布评论