CALCULATION SUBSTRINGS OF MATRIZ SEQUENCES

조회 수: 1 (최근 30일)
FRANCISCO
FRANCISCO 2013년 11월 21일
답변: Andrei Bobrov 2013년 11월 22일
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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 11월 22일
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);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by