How to vectorize a specific for-loop
Ältere Kommentare anzeigen
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 Kommentare
Jan
am 9 Dez. 2016
I've formatted the code for you. Please use the "{} Code" button the next time. Thanks.
Paolo Binetti
am 9 Dez. 2016
Akzeptierte Antwort
Weitere Antworten (1)
Roger Stafford
am 9 Dez. 2016
You might try the ‘hankel’ function:
n = numel(text);
nk = n-k+1;
pattern = hankel(text(1:nk),text(nk:n));
2 Kommentare
Jan
am 9 Dez. 2016
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
am 17 Dez. 2016
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!