MATLAB: CALCULATION SUBSTRINGS OF MATRIZ SEQUENCES

MATLABstring

Using the following code, I made a substrings of sequence. For example, i have the next sequence:
s = 0 (1) 1 (2) 1 (3) 0 (4) 1 (5) 1 (6) 0 (7) 1 (8) 0 (9) 0 (10) 0 (11) 1 (12)
(Numbers in parentheses indicate the order only) and applying the following code:
if true
% code
n=4;
m=numel(s)-n+1;
A=zeros(m,n);
idx=cell2mat(arrayfun(@(x) x:x+n-1,(1:m)','un',0));
out=s(idx)
end
I get:
0 (1) 1 (2) 1 (3) 0 (4)
1 (2) 1 (3) 0 (4) 1 (5)
1 (3) 0 (4) 1 (5) 1 (6)
0 (4) 1 (5) 1 (6) 0 (7)
1 (5) 1 (6) 0 (7) 1 (8)
1 (6) 0 (7) 1 (8) 0 (9)
0 (7) 1 (8) 0 (9) 0 (10)
1 (8) 0 (9) 0 (10) 0 (11)
0 (9) 0 (10) 0 (11) 1 (12)
But if instead of having a sequence, I have an array of sequences, as calculated the substrings of all sequences of the matrix, and store all of the substring in other matrix?
For example,
0 1 1 0 1 1 0 1 0 0 0 1
0 1 1 1 1 1 0 1 1 0 0 0
Many thanks

Best Answer

s = [0 1 1 0 1 1 0 1 0 0 0 1;
0 1 1 1 1 1 0 1 1 0 0 0];
sz = size(s);
k = 4;
s1 = s';
idx = bsxfun(@plus,hankel(1:k,k:sz(2))',reshape(sz(2)*(0:sz(1)-1),1,1,[]));
out = s1(idx);