How to vectorize a specific for-loop
이전 댓글 표시
I am trying to vectorize the for-loop hereafter. Would you have any hint? Thank you
for i = 1 : numel(text)-k+1 % "text" is a string
pattern(i,:) = text(i:i+k-1);
end
댓글 수: 2
Jan
2016년 12월 9일
I've formatted the code for you. Please use the "{} Code" button the next time. Thanks.
Paolo Binetti
2016년 12월 9일
채택된 답변
추가 답변 (1개)
Roger Stafford
2016년 12월 9일
You might try the ‘hankel’ function:
n = numel(text);
nk = n-k+1;
pattern = hankel(text(1:nk),text(nk:n));
댓글 수: 2
Jan
2016년 12월 9일
The vectorized version I've posted:
bsxfun(@plus, (1:numel(str) - k + 1).', 0:k-1)
is the core of the hankel function.
Paolo Binetti
2016년 12월 17일
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!