【问题标题】:find region in time with the largest successive number of values greater than one查找具有大于一的最大连续值的时间区域
【发布时间】:2012-12-07 13:40:18
【问题描述】:

我有两个时间序列:

dat = [0,2,3,0,2,2,0,0,1,0.8,3,4,6,7,4,4,3,0,1,3,2.2,0];
dat2 = dat+.5;
time = 1:length(dat);
plot(time,dat);
hold on;
plot(time,dat2,'r');

我想及时找到两个向量具有最大连续值大于一的区域。因此,对于这个特定示例,两个向量在 10 到 18 之间的值都大于 1。但是,在其他几个情况下,它们的值也大于 1。我可以通过首先生成一个矩阵来获得大于一的值的索引:

data = [dat',dat2'];

然后使用查找

r1 = data>1;

这将为我提供每个大于一的值的位置。接下来,我想找出在什么时间(在哪些行之间)保持最长持续时间的值 > 1。我怎样才能做到这一点?

【问题讨论】:

  • 您可以测试rBoth = dat>1 & dat2>1 是否同时处于上述状态。

标签: matlab find


【解决方案1】:

要查找最长运行值的索引,可以使用以下代码:

dat = [0,2,3,0,2,2,0,0,1,0.8,3,4,6,7,4,4,3,0,1,3,2.2,0];

id = find(dat>1);
x = [0 cumsum(diff(id)~=1)];

id(x==mode(x))

这会导致

ans =

    11    12    13    14    15    16    17

此代码可以与矩阵一起使用:

dat = [0,2,3,0,2,2,0,0,1,0.8,3,4,6,7,4,4,3,0,1,3,2.2,0];
dat2 = dat+.5;
data = [dat',dat2'];

id = find(all(data>1, 2));
x = [0; cumsum(diff(id(:))~=1)];

id(x==mode(x))

这段代码给出了两列中最长的大于一的值的索引:

ans =
    11
    12
    13
    14
    15
    16
    17

【讨论】:

  • 你让我对 matlab 的兴趣达到了顶峰
  • @LastCoder:如果您对 Matlab 有一点了解,那么输入时间(因此开发时间——每个字符都是潜在的错误)会大大减少。
  • 我假设 y 是 'dat'?
  • @Kate 不,是id。抱歉,这是一个错字。我固定在答案中。
  • 那么,这仍然适用于矩阵吗?我的例子是一个矩阵,这是一个向量?
【解决方案2】:

我不使用 matlab,但下面应该至少为您提供一个向量的最大范围。

rangeStart = 0;
rangeEnd = 0;
rangeLength = 0;
for i = 1:length(dat)
    for j = i+1:length(dat)
        % skip ranges smaller than the max
        if ((j-i)+1) <= rangeLength
            continue
        end
        % check to see if the range (i,j) meets the condition
        good = true;
        for x = i:j
            if dat(x) <= 1
                good = false;
            end
        end
        % the range meets the condition record the results
        if good
            rangeStart = i;
            rangeEnd = j;
            rangeLength = ((j-i)+1);
        end
    end
end

如果你想找到两个向量之间共享的最大范围,你会改变

            if dat(x) <= 1

            if dat(x) <= 1 || dat2(x) <= 1

除非出现任何语法错误,否则上述方法应该可以解决问题,可能有一个更高级(阅读效率更高)的 matlab 特定解决方案。

【讨论】:

    猜你喜欢
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多