HELP WITH ESTRACT SUBSTRING OF STRING

1 vue (au cours des 30 derniers jours)
FRANCISCO
FRANCISCO le 11 Déc 2013
Commenté : FRANCISCO le 11 Déc 2013
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

Réponses (3)

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

Andrei Bobrov
Andrei Bobrov le 11 Déc 2013
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 commentaires
FRANCISCO
FRANCISCO le 11 Déc 2013
Modifié(e) : FRANCISCO le 11 Déc 2013
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 le 11 Déc 2013
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

Connectez-vous pour commenter.


Jos (10584)
Jos (10584) le 11 Déc 2013
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])

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by