How do I split a string of numbers in n+1th successive intervals and put them in a array or matrix.
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Star Strider
2019년 4월 30일
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.
댓글 수: 0
추가 답변 (1개)
Stephen23
2019년 4월 30일
>> 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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 String에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!