Finding the indexes of multiple substrings within a larger string.
조회 수: 3 (최근 30일)
이전 댓글 표시
I’m trying to find the indexes of all two digit pairs in a very long string of numbers, say “c”. I can easily find all occurrences of one string at a time; for example strfind(c, ’00’)…strfind (c, ’01’). But I want a way to do this for all sets one hundred sets; 00 to 99. I tried this:
x=0:99;
dig=sprintf('%02d ',x);
%converts the vector 0to99 into a string with two digits, space between numbers
dub_dig=strsplit(dig);
%splits each pair into cells
dub_dig_str=string(dub_dig);
%converts to a string
How do I get this sequence of strings (dub_dig_str) to work in something like a for loop using the strfind function? When I try this it crashes. I would like to output a matrix of indexes of where each pair occurs, for all pairs.
Thanks
댓글 수: 0
채택된 답변
Stephen23
2023년 3월 24일
편집: Stephen23
2023년 3월 24일
idx = regexp(c,'\d\d') % no overlaps
idx = regexp(c,'\d(?=\d)') % with overlaps
댓글 수: 7
Stephen23
2023년 3월 26일
편집: Stephen23
2023년 3월 27일
"My goal is to output a separate row of indexes for each pair of numbers (one hundred total, 00to99), stating where each appears in c."
Aaah, so you actually want to compare the pairs against another set with a specific order, which is what you were achieving with the loop. Here is an alternative approach:
c = char(randi(+'09',1,123)) % random data
% Character pairs:
[T,U] = meshgrid('0':'9'); % all pairs
P = cellstr([T(:),U(:)]) % all pairs
Q = cellstr(c([1:end-1;2:end]).'); % data pairs
% Find indices of data pairs:
[~,X] = ismember(Q,P);
% Place indices into cell array:
Y = (1:numel(Q)).';
Z = accumarray(X,Y,[100,1],@(a){a})
Checking the indices of '00' and some random pair:
Z{1}
Z{strcmp(P,'23')}
You can probably do something simiar with table operations. Lets try it now:
D = cell2table(Q, 'VariableNames',"Pair");
D.Index = (1:numel(Q)).';
G = groupsummary(D,"Pair",@(a){a})
추가 답변 (1개)
Walter Roberson
2023년 3월 24일
c = 'a91bb48353'
mask = ismember(c, '0':'9');
odd_pair = find(mask(1:2:end-1) & mask(2:2:end)) * 2 - 1
even_pair = find(mask(2:2:end-1) & mask(3:2:end)) * 2
pair_starts_at = union(odd_pair, even_pair)
댓글 수: 2
Walter Roberson
2023년 3월 26일
c = char(randi([0 9], 1, 30) + '0')
C = c - '0';
odds = C(1:2:end-1) * 10 + C(2:2:end);
evens = C(2:2:end-1) * 10 + C(3:2:end);
odd_idx = (1:numel(odds)) * 2 - 1;
even_idx = (1:numel(evens)) * 2;
indices = accumarray([odds(:); evens(:)] + 1, [odd_idx(:); even_idx(:)], [], @(locs){locs});
populated = find(~cellfun(@isempty, indices));
[num2cell(populated-1), indices(populated)]
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!