How do I split a string of numbers in n+1th successive intervals and put them in a array or matrix.

1 vue (au cours des 30 derniers jours)
I need to split a string of numbers, for example-
123456123456123456123.....
into separate elements in the form
12345612
23456123
34561234
45612345
56123456
61234561
.....
and so on.....where the next element starts from the n+1th number in the string and size remains same.

Réponse acceptée

Star Strider
Star Strider le 30 Avr 2019
Another option is to use a loop:
n = '123456123456123456123'; % Convert To Character Array
len = 8; % Length Of Segments
for k = 1:numel(n)-len+1
s(k,:) = n(k:k+len-1); % Character Array
sn(k,:) = str2double(s(k,:)); % Numeric Array
end
Experiment to get the result you want.

Plus de réponses (1)

Stephen23
Stephen23 le 30 Avr 2019
>> S = '123456123456123456123';
>> [C,T] = regexp(S,'(\d)\d{0,6}','match','tokens');
>> D = strcat(C(:),vertcat(T{2:end},{''}));
>> D{:}
ans =
12345612
ans =
23456123
ans =
3456123

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by