通过重复重叠向量来创建矩阵 [英] Create matrix by repeatedly overlapping a vector
问题描述
我很难在MATLAB中编写以下代码: 假设您具有以下向量:
I'm having great difficulty coding the following in MATLAB: Suppose you have the following vector:
a
b
c
d
e
f
g
h
...
指定一个(均匀的)窗口大小,通过n
列(例如,L = 4
)创建以下尺寸为L
行的矩阵:
Specifying an (even) window size, create the following matrix of dimensions L
rows by n
columns (example, L = 4
):
a c e ...
b d f ...
c e g ...
d f h ...
更困难的是获取任意长度的向量,指定窗口数,并优化(最大化)窗口大小,以便转储向量末尾的值更少.
Even more difficult is taking a vector of arbitrary length, specifying the number of windows, and optimizing (maximizing) the window size so less values at the end of the vector are dumped.
推荐答案
在向量中创建索引矩阵.对于L = 4(我假设您与L/2重叠),索引为[1,2,3,4; 3,4,5,6; 5,6,7,8]等.令x = 1 :L,y = L/2,索引的向量为x + 0y,x + 1y,x + 2y,依此类推.
Create the matrix of indices into your vector. For L=4 (I assume you are overlapping by L/2), the indices are [1,2,3,4;3,4,5,6;5,6,7,8] etc. Let x = 1:L, y = L/2, the vector of indices is x+0y,x+1y,x+2y, and so on.
% let your initial data be in vector "data"
L = 4
N = floor(length(data)/(L/2))-1 % number of windows, or you specify this
mi = repmat(1:L,[N,1]) + repmat((L/2) * (0:(N-1))',[1,L]) % x + y * 0,1,2...
out = data(mi) % out is N-by-L, transpose to L-by-N if you like
这篇关于通过重复重叠向量来创建矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!