在Matlab中无法理解矩阵列的滑动窗口,一般情况和特殊情况 [英] Trouble Understanding Sliding Window for a column of a matrix, general and specific case in Matlab

查看:187
本文介绍了在Matlab中无法理解矩阵列的滑动窗口,一般情况和特殊情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am noob and i found very fragmentated information on stack on Slinding Window.

我有一个mXn矩阵,其中m是固定的(纬度,经度,ax,ay,az),n可以根据不同的对数进行更改.

I have a mXn matrix, where m is fixed(latitude, longitude, ax, ay, az), n could change from different logs.

1)如何在不提取向量然后对其进行分析的情况下仅为z创建滑动窗口?

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

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

4)我读了很多问题,我正在研究bsxfun在这种情况下的工作方式,但对我来说还不清楚(

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)

这是我所学到的,并试图进行总结.

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"

Matlab的基本方法(不是最有效的)是

The basic way on Matlab, not the most efficient, to do that is

1.定义矢量尺寸

2.定义窗口尺寸

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将为您提供标准偏差大于阈值的窗口的起始行. std_vals将为您存储所有标准偏差,以备日后需要时使用.

large_indexes will give you the starting row of the windows that have standard deviations larger than the threshold. std_vals will store all standard deviations for you, in case you need it later.

如果您希望窗口中所有行的索引都满足阈值,则可以在末尾添加

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

其中large_windows的每一行给出窗口中各行的索引.

where each row of large_windows gives indexes of the rows in the window.

这篇关于在Matlab中无法理解矩阵列的滑动窗口,一般情况和特殊情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆