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

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Star Strider
Star Strider il 30 Apr 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.

Più risposte (1)

Stephen23
Stephen23 il 30 Apr 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

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by