本文介绍了MATLAB中的“最近邻居"式插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一件小事,但是已经困扰了我一段时间,所以我想我应该让人群解决开始:)

This is a small thing but has been bothering me for a while now, so I thought I would let the crowd solving begin :)

我有一个带有时间戳的矩阵和一个对应的逻辑值(或1/0),即

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;

或其他,您明白了:)

现在,我如何(读:您)对of中的逻辑信息进行内插/外推,以使其与t中的时间戳匹配,但内插的逻辑始终假定最后一个或当前值,而不是将来的值一?

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?

不知道这是否有意义,但这是给定oft2

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]'

其中output的第一列是插值中使用的of的时间.如果我使用interp1和'nearest'算法,它将给出

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]'

这不是我想要的.

推荐答案

假设对向量进行了排序,您可以尝试一下,尽管我没有对它进行广泛的测试,但这似乎可以解决您的示例:

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,:);

希望有帮助.

interp1使用的默认方法是linear,它最适合您的情况,因为您不希望最近"邻居,而是想要第一个较低或相等的邻居(据我所知).因此,对内插时间戳索引进行简单的截短就可以得到结果.

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.

这篇关于MATLAB中的“最近邻居"式插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:14