在MATLAB中创建重叠和不重叠的滑动窗口 [英] Create overlapping and non-overlapping sliding windows in MATLAB

查看:667
本文介绍了在MATLAB中创建重叠和不重叠的滑动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从包含N元素的数组Data中创建重叠和不重叠的数据块.如何为任何N和任何blksze正确形成Data的子数组?以下代码用于非重叠块引发错误,因为创建子块时元素数超出了.例如,让Data = [1,2,3,4,5,6],然后

  • 对于重叠的情况,我应该得到:块大小blksze = 2,我会得到block1 = [1,2], block2 = [2,3], block3 = [3,4], block4 = [4,5], block5 = [5,6]

  • 对于不重叠的:块大小blksze = 2,我会得到block1 = [1,2], block2 = [3,4], block3 = [5,6]

代码段

N= 100;
n = 4;
Data = randi([1 n],1,N);
blksze = 10;
Nblocks = N / blksze;
counter = 1;
for i = 1 : Nblocks
    block{i} = Data(counter : counter + blksze - 1);
    counter = blksze + 1;
end

解决方案

要提取出重叠的块,建议使用 解决方案

To extract out overlapping blocks, I recommend using bsxfun to create the indices and subset the matrix whereas non-overlapping blocks you can simply use reshape.

Overlapping

ind = bsxfun(@plus, (1 : blksze), (0 : numel(Data) - blksze).');

The advantage of this method is that it uses broadcasting to generate the right indices per block. This would thus be a 2D matrix where each row are the indices required to grab the data for the right block and the number of columns is dictated by the block size.

Non-overlapping

ind = reshape(1 : numel(Data), [], numel(Data) / blksze).';

This simply reshapes the vector so that each row would be a unique set of indices that increases by 1 and the number of columns is dictated by the block size.


Finally, just index into Data to get what you need:

blocks = Data(ind);

Here's a running example using 6 elements:

>> rng(123); Data = rand(1, 6)

Data =

    0.6965    0.2861    0.2269    0.5513    0.7195    0.4231

With a block size of 2, or blksze = 2, here's what we get for both overlapping and non-overlapping:

>> blksze = 2;
>> indno = reshape(1 : numel(Data), [], numel(Data) / blksze).';
>> indo = bsxfun(@plus, (1 : blksze), (0 : numel(Data) - blksze).');
>> blockno = Data(indno)

blockno =

    0.6965    0.2861
    0.2269    0.5513
    0.7195    0.4231

>> blocko = Data(indo)

blocko =

    0.6965    0.2861
    0.2861    0.2269
    0.2269    0.5513
    0.5513    0.7195
    0.7195    0.4231

Caveat

This code does no error checking in that we assume that there are enough blocks to capture all of your data. If you have the number of elements in Data to be incompatible with the block size to capture all of the data in blocks of all the same size, an error will occur upon indexing.

这篇关于在MATLAB中创建重叠和不重叠的滑动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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