HELP WITH ESTRACT SUBSTRING OF STRING

조회 수: 1 (최근 30일)
FRANCISCO
FRANCISCO 2013년 12월 11일
댓글: FRANCISCO 2013년 12월 11일
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에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by