数值数组映射到第二个数值数组 [英] mapping of numeric array onto second numeric array

查看:175
本文介绍了数值数组映射到第二个数值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的,因为我想知道,如果你们可能有一个建议
在MATLAB下面的数组映射的问题。

I'm writing, because I was wondering if you guys might have a suggestion for the following "array mapping" problem in MATLAB

我有一个时间阵列覆盖,一年在一分钟内(T1)和步骤
另一个时间阵列(T2)是不均匀分布,而不是
(必然)重叠与T1。
一个例子是:

I have a time array covering, a year in steps of one minute (T1) and another time array (T2) that is inhomogeneously distributed and not (necessarily) overlapping with T1. An example would be:

T1 = [1-Jan-2011 00:01:23, 1-Jan-2011 00:02:23.... end of year 2011]
T2 = [1-Jan-2011 00:04:12, 1-Jan-2011 03:014:54, ....]

T1和T2实际上是 datenum 格式,但我想在这里给出一个明显的例子。

T1 and T2 are actually in datenum format, but I wanted to give a clear example here.

两者的长度是不相同的,(长度(T1)〜5 *长度(T2)),
但我知道,T2的任何两个元件在相同的时间间隔内
T1。我的意思是T2的元素将被永远唯一所确定
T1的之一。

The length of the two is not the same, (length(T1) ~ 5*length(T2)), but I know that no two elements of T2 are within the same interval in T1. I mean that an element of T2 will be always uniquely identified by one of T1.

我想要做的是(高效快速=)映射到T2 T1,所以
我有一组索引IDX,使得 T1(IDX(N))是最接近
时间点 T2(N)。我已经有一个例行这样做,但它是一个
有些慢。

What I want to do is (efficiently=quickly) mapping T2 onto T1, so that I have a set of indexes idx such that T1(idx(n)) is the closest point in time to T2(n). I already have a routine doing it, but it's a little slow.

建议?

非常感谢提前!
里卡尔多·

Thanks a lot in advance! Riccardo

推荐答案

据我所看到的, datenum 是简单的数字的结果。

As far as I can see, the result of datenum are simple numbers.

[~,idx1]=sort([T1+offset,T2]);
idx = find(idx1>length(T1));
idx = idx - (0:length(idx)-1);

如果你离开了偏移(或使用 0 ),这会给你的每个元素 T2 如果 T1 这是更大的最小元素的索引。为了得到一个最接近,在 T1 添加间隔长度一半 T1 (即 datenum 当量)。

If you leave out the offset (or use 0), this will give you for each element of T2 the index of the smallest element if T1 that is larger. To get to the one that is closest, add half the interval length in T1 to T1 (i.e. the datenum equivalent of half a minute).


如果 T1 不包括等距步骤,一个是可以用包含每个区间的中段<$​​ C $ C> T1矢量尝试代替

[edit] If T1 does not consist of equidistant steps, one could try with a vector containing the middle of each interval in T1 instead.

T1m = [(T1(1:end-1) + T1(2:end))/2];
[~,idx1]=sort([T1m,T2]);
idx = find(idx1>length(T1m)) - (0:length(T2)-1);

[/编辑]

这是如何工作:

我们首先梳理各时间点的矢量,则忽略实际结果(变量名称替换,例如 T )。 的第二个返回值排序是原始数组中的排序数组中的每个条目的索引。我们想知道从哪里 T2 的那些结束了,即那些原来,级联阵列中的 [T1 T2] T1 ,这是 IDX 从第二行比值的数量较大的指数。现在这些指数是指将合并的阵列,这意味着相对于 T 1 它们是用于第一元件正确,关闭由一个用于第二(自的第一个元素的元素 T2 在之前第三(因为 T2的两个元素来之前)抛出),关由两个.. ,这是我们在第三行纠正。

We first sort the vector of all time points, then ignore the actual result (replace ~ by a variable name, e.g. T if you want to use it somehow). The second return value of sort is the index of each entry of the sorted array in the original array. We want to know where the ones from T2 ended up, i.e. those that in the original, concatenated array [T1 T2] have an index larger than the number of values in T1, which is the idx from the second line. Now these indices refer to the elements of the combined array, which means relative to T1 they are correct for the first element, off by one for the second (since the first element of T2 was thrown in before), off by two for the third (since two elements of T2 come before)..., which we correct in the third line.

您可以在二,三线结合 IDX =找到(IDX1&GT;长度(T1)) - (0:长度(T2)-1);

You can combine the second and third line to idx = find(idx1>length(T1)) - (0:length(T2)-1);

这篇关于数值数组映射到第二个数值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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