Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create matrix by repeatedly overlapping a vector

I'm having great difficulty coding the following in MATLAB: Suppose you have the following vector:

a   
b
c
d
e
f
g
h
...

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.

like image 606
janon128 Avatar asked Dec 21 '22 14:12

janon128


1 Answers

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
like image 157
engineerC Avatar answered Jan 07 '23 04:01

engineerC