How to modify the following code
Show older comments
good , using the following code I want to create all the substrings of a sequence s . Eg
s = [ 1 1 1 0 0 1 0 1 ]
using the following code , where n = length substrings :
if true
% code
N = length ( s ) ; % number rows
n = 4 ;
A = Hankel(1: N -n +1 , N- n +1: N);
k = 0: n-1;
idx = [ ] ;
for ii = 1 : size ( A, 1 )
p = A ( ii , :) ;
while p (end , end) + k ( end) < = N
p = [p , p ( end , :) + k] ;
end
idx = [ idx , p ] ;
end
[ j1 , j2 , j2 ] = unique ( s ( idx ) , ' rows ');
end
The code returns me an array with the result of combinations in this sequence:
0 0 1 0
0 1 0 1
1 0 0 1
1 0 1 1
1 1 0 0
1 1 1 0
My question is this : How I can change the code so that instead of a single sequence s me , I make the rows of a matrix. For example , I want to calculate the substrings of length 4,5 ... n of the rows of the following matrix :
1 1 1 0 0 1 0 1
0 1 1 0 0 0 0 0
and that all combinations the same length of both rows store in a single array , using the above code . How do? thank you very much
4 Comments
Roger Stafford
on 14 Nov 2013
Edited: Roger Stafford
on 14 Nov 2013
It is important that you carefully define what you mean by a "substring" and show us in detail how that accounts for the six rows of your result. Apparently they must in some sense be contiguous elements of s but to account for the 1 0 1 1 string it is necessary to expand s with another copy of itself. But then why aren't 0 1 1 1 and 1 1 1 1 allowed? I hope you won't define a "substring" as whatever result is given by your code.
FRANCISCO
on 14 Nov 2013
FRANCISCO
on 14 Nov 2013
Roger Stafford
on 15 Nov 2013
I should point out that the line with
unique(s(idx),'rows');
will not work as it stands. You would need to write
unique(reshape(s(idx),n,[])','rows')
to get the results you claim.
Also if the length(s) is sufficiently large that the while-loop executes twice in succession, then the line
p = [p,p(end,:)+k];
will fail. It should be replaced by
p = [p,p(end,end-n+1:end)+k];
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!