How to modify the following code

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 Commenti

Roger Stafford
Roger Stafford il 14 Nov 2013
Modificato: Roger Stafford il 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.
okei, I define the substring according idx. The way to create the substring is by the number of combinations as follows (numbers in parentheses indicate the order);
  s = [1 (1) 1 (2) 1 (3) 0 (4) 0 (5) 1 (6) 0 (7) 1 (8)];
then when you run the above code, the array idx shows me the order:
1 2 3 4
1 3 5 7
2 3 4 5
2 4 6 8
3 4 5 6
4 5 6 7
5 6 7 8
that by using the code:
if true
% code
[j1, j2, j2] = unique (s (idx), 'rows');
end
I estimated subsequences previous 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
I do this for a sequence s, but now I have an array with multiple sequences, for example:
1 1 1 0 0 1 0 1
0 1 1 0 0 0 0 0
and save the result of the calculation of both sequences distinct array to another array
Many thanks
I can not find solution
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];

Accedi per commentare.

 Risposta accettata

To use more than one row in s, in place of N = length(s) you can do this:
[M,N] = size(s);
Then use your code (hopefully corrected in the while-loop as I recommended earlier) to create the row vector idx. Then do this:
m = length(idx);
idx = repmat(idx,1,M)+reshape(repmat(0:N:(M-1)*N,m,1),1,[]);
s = s';
j1 = unique(reshape(s(idx),n,[])','rows');

4 Commenti

Very good,
and realize the code as follows:
use the following matrix:
1 0 0 1 1 1 0 0
0 0 0 0 1 0 0 1
if true
% code
[M,N] = size(s);;%numero filas
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,end-n+1:end)+k];
end
idx=[idx;p];
end
m = length(idx);
idx = repmat(idx,1,M)+reshape(repmat(0:N:(M-1)*N,m,1),1,[]);
s = s';
j1 = unique(reshape(s(idx),n,[])','rows');
end
and Matlab gives me error:
Error using vertcat CAT arguments dimensions are not consistent.
I'm doing wrong?
I've also tried placing it in the following way:
if true
% code
[M,N] = size(s);;%numero filas
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,end-n+1:end)+k];
end
m = length(idx);
idx = repmat(idx,1,M)+reshape(repmat(0:N:(M-1)*N,m,1),1,[]);
end
end
But matlab, the end of the loop gives me error:
"Error using + Matrix dimensions must agree."
I also I can the following code to reform without p, but neither was able to calculate for an array:
if true
% code
N = length(s);
n = 4;
A = hankel(1:N-n+1,N-n+1:N);
k = 0:n-1;
c = ceil((N - A(:,end) + 1)/k(end));
i2 = cumsum(c);
i1 = i2 - c + 1;
idx = zeros(i2(end),n);
for jj = 1:N-n+1
idx(i1(jj):i2(jj),:) = bsxfun(@plus,A(jj,:),(0:c(jj)-1)'*k);
end
[j1,j2,j2] = unique(s(idx),'rows')
end
Many thanks
You made one change in your original code and that is apparently what is causing you the trouble. In your original code you had:
idx = [idx,p];
just after you exit the while-loop and just before the end of the for-loop. However, in the more recent version which failed with the error message "Error using vertcat CAT arguments dimensions are not consistent", you had written instead:
idx = [idx;p];
with a semicolon instead of a comma. That would invalidate the coding which I suggested to you. My assumption was that idx would emerge from the for-loop as a vector with only a single row, as you had it originally. I recommend you replace the semicolon with a comma and try it out again.

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by