【问题标题】:sliding window to find minimums in matlab 2014a在matlab 2014a中找到最小值的滑动窗口
【发布时间】:2019-08-21 16:00:21
【问题描述】:

我是 matlab 新手,我想知道有什么方法可以让我做到以下几点:

在我的数据的每 180 个样本中获取最小值索引列表,长度为 18000 个样本

这就是我的方式,这就是我过去在 python 中的方式(使用 numpy.where),但现在我正在尝试在 matlab 中进行。请帮忙。

dips = numpy.where((smoothEnvelop[180:-180] < 7))[0] + 180 #7 is a threshold example

感谢

【问题讨论】:

    标签: python matlab sliding-window


    【解决方案1】:

    以下是使用循环的解决方案。它还包含定义步长的可能性。

    % Input.
    x = round(rand(20, 1) * 100)
    
    % Window size.
    ws = 5;
    
    % Step size.
    ss = 1;
    
    % Number of elements.
    nx = numel(x);
    
    % Number of result elements.
    ny = numel(1:ss:(nx - ws + 1));
    
    % Initialize output.
    y = zeros(ny, 1);
    
    % Calculate output.
    jj = 1;
    for ii = 1:ss:(nx - ws + 1)
      ww = x(ii:(ii + ws - 1));
      [~, yLoop(jj)] = min(ww);
      yLoop(jj) = yLoop(jj) + ii - 1;
      jj = jj + 1;
    end
    
    % Output.
    y
    

    存储在y 中的最小值的索引是相对于原始输入x

    ws = 5ss = 1 的输出:

    x =
       88
       74
       96
       31
        6
       67
       98
       92
       69
       49
       12
       28
       43
       87
       68
       49
       20
       98
       83
       62
    
    y =
        5
        5
        5
        5
        5
       10
       11
       11
       11
       11
       11
       12
       17
       17
       17
       17
    

    编辑

    我添加了一个使用索引的版本,这对于大型输入来说速度更快。

    % Input.
    x = round(rand(20000, 1) * 100);
    
    % Window size.
    ws = 5;
    
    % Step size.
    ss = 1;
    
    % Number of elements.
    nx = numel(x);
    
    % --- Solution using loop --- %
    
    % Number of result elements.
    ny = numel(1:ss:(nx - ws + 1));
    
    % Initialize output (loop).
    yLoop = zeros(ny, 1);
    
    tic
    % Calculate output.
    jj = 1;
    for ii = 1:ss:(nx - ws + 1)
      ww = x(ii:(ii + ws - 1));
      [~, yLoop(jj)] = min(ww);
      yLoop(jj) = yLoop(jj) + ii - 1;
      jj = jj + 1;
    end
    tLoop = toc;
    
    % Output (loop).
    yLoop;
    
    % --- Solution using indexing --- %
    
    tic
    % Calculate indices.
    ind = 1:ss:(nx - ws + 1);
    ind = repmat(ind, ws, 1) + ([0:(ws - 1)].' * ones(1, numel(ind)));
    
    % Calculate output (indexing).
    [~, yIndexing] = min(x(ind));
    yIndexing = (yIndexing + (0:ss:(ss * (size(ind, 2) - 1)))).';
    tIndexing = toc;
    
    % Compare loop and indexing version.
    fprintf("yLoop == yIndexing: %d\n", sum(yLoop == yIndexing) == ny);
    fprintf("yLoop time: %f s\n", tLoop);
    fprintf("yIndeing time: %f s\n", tIndexing);
    

    对于n = 20000ws = 5ss = 1,我们得到以下时间:

    yLoop time: 0.672510 s
    yIndeing time: 0.004466 s
    

    【讨论】:

    • 谢谢您,先生,但是当我留下来时,我想获得那些最小值的索引,我使用了 find(y(jj)) 但我想我仍然不知道如何正确使用它,因为它给了我全部 1
    • 啊,对不起,我错过了!将尽快提供更新的解决方案。
    • 更新了我的解决方案 - 再次抱歉最初的不完整答案!
    • @AmineChadi 现在,两个版本 - 循环和索引 - 应该都能正常工作。
    【解决方案2】:

    data = [6 7 7 3 6 1 3 7 2 1 0 8]; % example data
    N = 4; % block size
    

    对于不相交的块,只需重塑一个矩阵,使每个块变成一列,然后计算每列的arg min:

    [~, indices] = min(reshape(data,N,[]), [], 1);
    

    给予

    indices =
         4     2     3
    

    对于滑动块,创建一个索引矩阵,然后沿每列计算 arg min:

    [~, indices] = min(data(bsxfun(@plus, 1:numel(data)-N+1, (0:N-1).')), [], 1);
    

    给予

    indices =
         4     3     4     3     2     1     4     4     3
    

    【讨论】:

      猜你喜欢
      • 2012-06-03
      • 2012-05-30
      • 2013-02-06
      • 1970-01-01
      • 2017-04-03
      • 2015-06-03
      • 1970-01-01
      • 2011-05-06
      • 2017-05-22
      相关资源
      最近更新 更多