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

1 view (last 30 days)
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.

Accepted Answer

Star Strider
Star Strider on 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.

More Answers (1)

Stephen23
Stephen23 on 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

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!