HELP WITH ESTRACT SUBSTRING OF STRING

1 次查看(过去 30 天)
good, I wish to raise my problem, because I know how to do. I, for example have a long string as follows:
[3 -1 -1 -1 -1 -1 1 -1 -1 -1 0 -1 -1 -1 -1 4 -1 0 -1 -1 -1 0 -1 -1 3 -1 -1 -1 -1 -1 -1 4 -1 -1 0
-1 1 -1 -1 -1 0 -1 1 -1 -1 -1 -1 -1 -1 1 -1 0 -1 -1 4……..
What I want to do is the following:
1) extracting substrings so that starts for next 3/4 and ends 3/4 including the upcoming latter. For example:
[-1 -1 -1 -1 -1 1 -1 -1 -1 0 -1 -1 -1 -1 4]
Next substring;
[-1 0 -1 -1 -1 0 -1 -1 3]
Next substring;
[-1 -1 -1 -1 -1 -1 4]
And so on

回答(3 个)

Walter Roberson
Walter Roberson 2013-12-11
regexp(String, '(?<=[34]).*?[34]', 'match')
Or so I figure as I drift off to sleep.

Andrei Bobrov
Andrei Bobrov 2013-12-11
a = [3 -1 -1 -1 -1 -1 1 -1 -1 -1 0 -1 -1 -1 -1 4 -1 0 -1 -1 -1 0 -1 -1 3 -1 -1 -1 -1 -1 -1 4 -1 -1 0 -1 1 -1 -1 -1 0 -1 1 -1 -1 -1 -1 -1 -1 1 -1 0 -1 -1 4];
l = a == 3 | a== 4;
l(2:end-1) = circshift(l(2:end-1),[0 1]);
l(end) = 0;
out = accumarray(cumsum(l(:)),a(:),[],@(x){x});
  2 个评论
FRANCISCO
FRANCISCO 2013-12-11
编辑:FRANCISCO 2013-12-11
I wanted to ask something else I do not know how I can do it :
The end result should look like the following , for example :
a) [ -1 -1 -1 0 -1 1 -1 4 ] started with this sequence
MATRIX1 ( the length of the substring is n = 3)
PATTERN________________OCCURS________________________-Nº(-1)
0 1 4---------------------------------------1----------------------------------------------------5
eliminating pattern we get (-1 ) So if another pattern appears the same size as this matrix include , for example :
b) [-1 -1 1 -1 0 3]
PATTERN________________OCCURS________________________-Nº(-1)
0 1 4---------------------------------------1----------------------------------------------------5
1 0 3---------------------------------------1----------------------------------------------------3
So if another pattern appears, but of varying length I store in another array , for example :
c ) [ 1 -1 0 -1 -1 -1 1 -1 4]
MATRIX2 ( the length of the substring is n = 4)
PATTERN________________OCCURS________________________-Nº(-1)
1 0 1 4---------------------------------------1---------------------------------------------------5
My question is, how can I remove the numbers with a loop (-1) for the pattern, and I store it in another array each pattern of the same length?
Before deleting (-1) should make a count and associate to pattern.
Many thanks
FRANCISCO
FRANCISCO 2013-12-11
To count (-1) I used:
if true
% code
n=length(out);
for i=1:n;
gg=out(i,1);
gg=cell2mat(gg);
menos1(i)=numel(gg(gg==-1));
end
menos1=menos1';
end
And to remove (-1) I tried to use this code but matlab gives me error
if true
% code
out1=out;
n=length(out1);
for i=1:n;
gg=out1(i,1);
gg=cell2mat(gg);
gg(gg(i)==-1)=[];
end
end
Many thanks

请先登录,再进行评论。


Jos (10584)
Jos (10584) 2013-12-11
If you have version 2013b, there is a function called strsplit , that might work on numerical arrays (like strfind used to do). I cannot test this, though.
C = strsplit(str,[3 4])

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by