【问题标题】:Trouble Understanding Sliding Window for a column of a matrix, general and specific case in Matlab麻烦理解矩阵列的滑动窗口,Matlab中的一般和特定情况
【发布时间】:2017-10-24 00:37:43
【问题描述】:

我是菜鸟,我在 Slinding Window 的堆栈上发现了非常零散的信息。

我有一个 mXn 矩阵,其中 m 是固定的(纬度、经度、ax、y、az),n 可以从不同的日志中改变。

1) 我怎样才能只为 az 创建一个滑动窗口而不提取向量然后分析它?

2) 如果我想保存 az 标准差超过定义阈值的所有行,我该怎么做?

3) 如果日志长度不固定,我该如何处理? (例如,一个文件包含 932 行,另一个包含 953 行)

4) 我读了很多问题,我正在研究 bsxfun 在这种情况下是如何工作的,但对我来说还不清楚(in this examples 我只是不明白 根据窗口大小创建一个新矩阵,然后分析新矩阵)(最后一个问题与我的土木工程师背景密切相关)


这是我学到的,并试图汇总。

滑动窗口是一个强大的工具,可以分析信号或图像。 当我试图向我的女朋友描述我在做什么时,我解释了 “就像用放大镜看书, 放大镜有一个定义的尺寸,你分析文本”

Matlab 上的基本方法,不是最有效的,这样做是

1.定义矢量尺寸

2。定义您的窗口尺寸

3.定义步数

这是我写的一个基本示例

a= randi(100,[1,50]);        %Generic Vector
win_dim=3;                   %Generic window size

num_stps=(length(a)-win_dim) %number of "slides", we need to subtract win_dim to avoid that the window will go over the signal 
threshold=  15 %Generic number only for the example
for i= 1:num_stps
    mean_win(i)=mean(a(i:i+win_dim -1); %we subtract 1 or we make an error, and the code analyzes a segment bigger than one unit 
    std_win(i)=std( a(i:i+win_dim -1);  %example for i=2 if we don't subtract 1 our segment starts from 2 until 5, so we analyze 
                                        % 2 3 4 5, but we defined a window dimension of 3
    If std_win(i)> threshold
    std_anomalies=std_win(i)             %here i think there is an error                                
end     

这样代码会在整个信号上滑动,但窗口会重叠

如何确定“重叠率”(或幻灯片增量)?

我们可以将其定义为“两个相邻窗口共享多少信息?” 我所做的以下示例是完全错误的,但我在问这里之前尝试编写一些代码,目标本来希望是 一半的片段或没有重叠

的重叠
%Half segment overlap

a= randi(100,[1,20]); %Generic Vector
win_dim=4;  %generic window size    
%v is the increment vector in our case we desire to have 50% of overlap 
l= win_dim
if  l%2==0
    v=l/2
else 
    v=(l+1)/2
end     

for i= 1:num_stps
    if (i==1)
    mean_win(i)=mean(a(1:1+win_dim -1);
    else 
    mean(i)= mean(a (i+v:i+win_dim+v-1);
end

【问题讨论】:

    标签: matlab sliding-window bsxfun


    【解决方案1】:

    我喜欢这个问题的创造性方法! :) 这就是你要找的吗?

    a = rand(100, 5); % the data
    
    window_size = 5; % size of the window
    overlap = 2; % desired overlap
    step = window_size - overlap; % compute the step
    
    threshold = 0.3; % large std threshold
    
    std_vals = NaN(size(a, 1), 1);
    for i=1:step:(size(a, 1) - window_size)
        std_vals(i) = std(a(i:(i+window_size-1),5)); % calculate std of 5th col
    end
    
    % finding the rows with standard deviation larger than threshold
    large_indexes = find(std_vals>threshold);
    

    large_indexes 将为您提供标准偏差大于阈值的窗口的起始行。 std_vals 将为您存储所有标准差,以备日后需要。

    如果你希望窗口中所有行的索引都满足你的阈值,你可以在最后添加这个

    large_windows = zeros(numel(large_indexes), window_size);
    for i=1:window_size
        large_windows(:,i) = large_indexes + i - 1;
    end
    

    large_windows 的每一行给出窗口中行的索引。

    【讨论】:

    • 我需要澄清一下。你选择Nan来初始化std_vector?为什么?我是第一次看到 double : 它是如何工作的吗?我总是只看到一个如何保存窗口内具有满足不等式关系的 (i,5) 元素的所有行,而不仅仅是单个元素? Tnx 为您的时间和我的愚蠢问题感到抱歉
    • 给定行没有可用的值,因此 NaN 对我来说是最合乎逻辑的选择。它使测试阈值变得更容易。对于冒号,请参见此处:uk.mathworks.com/help/matlab/ref/colon.html。我已经编辑了答案以分组获取所有行号。我建议您阅读文档并自己尝试一下,这是正确学习的唯一方法。
    • 我如何在我的情况下使用 bsxfun(@plus 作为滑动窗口,就像在这个例子中 link 一样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多