简体   繁体   English

在Matlab中无法理解矩阵列的滑动窗口,一般情况和特殊情况

[英]Trouble Understanding Sliding Window for a column of a matrix, general and specific case in Matlab

I am noob and i found very fragmentated information on stack on Slinding Window. 我是菜鸟,我在Slinding Window的堆栈上发现了非常零散的信息。

I have a mXn matrix, where m is fixed(latitude, longitude, ax, ay, az), n could change from different logs. 我有一个mXn矩阵,其中m是固定的(纬度,经度,ax,ay,az),n可以根据不同的日志进行更改。

1) How can i create a sliding window only for az without extracting the vector and then analyzing it? 1)如何在不提取向量然后对其进行分析的情况下仅为z创建滑动窗口?

2) If i want to save all the rows where the az standard deviation go over a defined thresholds how can i do that? 2)如果我要保存所有的行的标准差标准偏差超过定义的阈值,我该怎么办?

3) If logs length is not fixed how can i deal with that? 3)如果日志长度不固定,我该如何处理? (ex. one file contains 932 rows, another 953) (例如,一个文件包含932行,另一文件953个行)

4) I read a lot of questions, i am studying how bsxfun works in this case but is very unclear for me ( in this examples i only undestood that a new matrix is created, based on the window size and then the new matrix is analyzed)(this last question is strongly related to my civil engineer background) 4)我读了很多问题,我正在研究bsxfun在这种情况下如何工作,但对我来说还不清楚( 在此示例中,我只是不了解根据窗口大小创建新矩阵,然后分析新矩阵) )(最后一个问题与我的土木工程师背景密切相关)


Here what i learned, and tried to aggregate. 在这里,我学到了什么,并尝试进行汇总。

Sliding Window is a powerful tool that allows to analyze a signal or an image. 滑动窗口是功能强大的工具,可用于分析信号或图像。 When I tried to describe to my girlfriend what I was doing I explained “Is like reading a book with a magnifier, the magnifier has a defined dimension and you analyze the text” 当我试图向女友描述我在做什么时,我解释说:“就像用放大镜看书,放大镜具有确定的尺寸,您可以分析文本”

滑动窗口

The basic way on Matlab, not the most efficient, to do that is Matlab的基本方法(不是最有效的)是

1. Define your vector dimensions 1.定义向量尺寸

2. Define you window dimension 2.定义窗口尺寸

3. Define the number of steps 3.定义步骤数

Here a basic example that i wrote 这是我写的一个基本例子

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     

This way the code slides over the entire signal, but windows will overlap . 这样,代码可以在整个信号上滑动, 但是窗口会重叠

How to decide the "overlap ratio" (or slide increment)? 如何确定“重叠率”(或滑动增量)?

We can define this like "how much informations two adjacent windows share?" 我们可以将其定义为“两个相邻窗口共享多少信息?” 半段重叠 The following examplei have done is completely wrong, but i tried to code something before asking here, the goal would have liked to be an overlap for Half of the segment or no overlap 下面的示例我做的是完全错误的,但是我在尝试在此处询问之前尝试编写一些代码,目标希望是该段的一半重叠或不重叠

%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

I like the creative approach to the question! 我喜欢这个问题的创造性方法! :) Is this is what you are looking for? :)这是您要找的东西吗?

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 will give you the starting row of the windows that have standard deviations larger than the threshold. large_indexes将为您提供标准偏差大于阈值的窗口的起始行。 std_vals will store all standard deviations for you, in case you need it later. std_vals将为您存储所有标准偏差,以备日后需要时使用。

If you want indexes of all the rows in the window satisfying your threshold, you can add this at the end 如果要使窗口中所有行的索引都满足阈值,可以在末尾添加

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

where each row of large_windows gives indexes of the rows in the window. 其中large_windows每一行都给出了窗口中各行的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM