首页 文章

找到具有大于1的最大连续数值的时间区域

提问于
浏览
2

我有两个时间序列:

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');

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

data = [dat',dat2'];

然后使用find

r1 = data>1;

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

2 回答

  • 7

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

    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))
    

    此代码提供两列中最长运行值大于1的索引:

    ans =
        11
        12
        13
        14
        15
        16
        17
    
  • 0

    我不使用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特定解决方案 .

相关问题