Returning specific string from a given string that are found between two given substrings ?

Hi guys !
I'm in a process for doing a function in matalb that does this thing:
it gets as input array of integers values that are zero or one, the input is a vector or matrix 1XN or NX1 (i.e array) it includes 0 or 1 values.
For instance:
input1=[0 0 0 0 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 1 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 ]; % it's array of integer values of 0 or 1
I want to search for specific substrings (two substring) which it's given as constant substrings , the two substrings are constant in my case which they are: substring1=[0 1 0 1] % it's array of integer that its values is just 0 or 1 -binary values-
substring2=[1 0 0 1] % it's array of integer that its values is just 0 or 1 -binary values-
it's always that first occur/appear substring1 and then substring2 appears in my input1, and if the opposite appear (this means substring2 is appeared before substring1) then I do nothing implicitly I just look first at substring1 if found then I look to substring2 ..so I want to search for my giving substrings in my input1 on two substrings respectively (first substring1 and then substring2) and to return at each two occurrence of my given substrings in input1 all offset data that are between the two occurrence of my giving substrings, this means to return all offset data that are found between 0101 and the other substring 1001, it could be many returned times of all offset data which it depends if 0101 and 1001 are occured more than once, to clarify more : I will explain in detail what I explained before and try by an example to clarify my qestion.
my function that I want to implement gets as inputs :
first input: a given string -array/vector of chars-
input1=[0 0 0 0 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 1 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 ];
second input is substring called :
substring1=[0 1 0 1];
third input is substring called :
substring2=[1 0 0 1];
So if I have input1 as I mentioned above, and constant substrings as I showed in my example above then what I want to do is to find two each occurrence of given substrings respectively(first substring1 occurs and then substring2 occurs) in my input1 and all offset data between first substring1=[0 1 0 1] and second substring2=[1 0 0 1 ] are returned in one row as matrix output and it could be more than one row of matrix output which it depends of how many times the two substrings are appeared/occured in my input1. so implicitly my function returns a matrix(two dimensional array) size mXN which number of rows is depend of how many times does the two substrings respectively(first substring1 and then substring2) are occured in my input1.
so the output:
Output=[0 0 1 1 1 1 1 1 1 1 1 1 0 ; 0 0 0 0 0 0 0 0 0 0 0 0 0];
so we see here occurence of 0101 and 1001 two times, first time 0101 and 1001 occured and then second time occured 0101 and 1001 in my input1 , so in my case number of rows of my output matrix is 2 (because 0101 and 1001 occured twice) and all offset data that found between each occurrence(between 0101 and 1001) are:
first row of my output matrix is : 0 0 1 1 1 1 1 1 1 1 1 1 0 and this because 0101 and 1001 occured as first time respectively(0101 first and then 1001), so first row in my output matrix represents all data offset that found between first occurence .
second row of my output matrix is :0 0 0 0 0 0 0 0 0 0 0 0 0 and this because 0101 and 1001 occured as second time respectively(0101 first and then 1001), so second row in my output matrix represents all data offset that found between second occurence .
the output is a matrix MXN that every row represent all offset data that are found between eachoccurrence of 0101 and 1001, all offset data that are found between 0101 and 1001 are on the same size (this means that N which is number of columns of matrix output is equal for each occurrence of 0101 and 1001.
first row represents first occurrence of 0101 and 1001 (all offset data that found between 0101 and 1001) , second row represents second occurrence of 0101 and 1001(all offset data according between 0101 and 1001) ..etc , so number of rows are representing implicitly number of occurrence of my two given substrings.
there can't be overlab between first occurrence of my substring to next occurrence of my substring in my input1, this means I will always return at every occurrence of my substring all the offset data that found between each occurrence of my two given substrings in my input1.
Any help please how do I implement that function in matlab? appreciate alot your effort for assistance !
thanks alot for helpers !

 Accepted Answer

input1=[0 0 0 0 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 1 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 ];
substring1=[0 1 0 1];
substring2=[1 0 0 1];
Convert arrays to char() stirngs...
ss1=char(ss1+'0');
ss2=char(ss2+'0');
>> extractBetween(char(input1+'0'),ss1,ss2)
ans =
2×1 cell array
{'0011111111110'}
{'0000000000000'}
>>

6 Comments

thanks ! but I want to take every row matrix of the output, in my case the output is the output of exextractBetween and it's matrix .. so at each row of that matrix of my output I call the other function, the otherfunction called Build() I implemented it, and it takes in its input every row of the output matrix of extractBetween .. so How can I implement that? I tried to write this code matlab but it doesn't work, appreciate if you could instruct me to fix it.
ss1=char(ss1+'0');
ss2=char(ss2+'0');
>> Result=extractBetween(char(input1+'0'),ss1,ss2)
result =
2×1 cell array
{'0011111111110'}
{'0000000000000'}
>> for i=1:size(Result)
output(i)=Build(Result(i:)) %here I want to call the other function Build that I %implemented by every row of the matrix Result as input to Build.
>>
"but it doesn't work, appreciate if you could instruct me to fix it."
We can't fix what we can't see...we have no way to know what your function expects for inputs nor what its output(s) are for the barest minimum of starters...not even an error message from which to try to guess...
Sorry for not clearing out what my function return/input.
my function Build(substring) is gets as input every row of the matrix Result - Result is output matrix of function extractBetween . row function Build returns an integer value .. (it does something and return a value which it's integer), those values that Build function returns is storing them on array called output !
what Im trying to do is to call the function Build by inputing to it every row of my matrix from function extractBetween, so I want to call the function Build and input to it all the rows of my Result matrix that's an output of extractBetween. in brief, I need to run on all rows of my matrix that's output of extractBetween and at each row I input it to function Build respectively, the output of Build function over each row is stored in array called output .
How could I implement that in matlab?!
Just like you did except fix the typo of the superfluous : in the subscript on the Result array.
BUT, in MATLAB, it's better to write the function to accept a vector input -- depending on what it does, it's quite possible there's no need for an explicit loop in that function at all which eliminates the loop entirely.
But again, w/o knowing what it is the function is doing with the input, we can't answer that...
I understand you, but I said that function Build is take as its input substring which iin my case I must input every row of my matrix output of function extractBetween(char(input1+'0'),ss1,ss2) .
So Im asking how can I call my function Build by every row of my output matrix of function extractBetween(char(input1+'0'),ss1,ss2) ? thanks alot.
Yes, you said that before and I already answered...but "The MATLAB way!" is to write functions that accept vectors/arrays as their inputs to minimize explicit looping constructs at the higher code levels...with the side benefit one can often elminate all explicit loops by utilizing the language the way it was designed to be -- as a "MATrix LABoratory"
Properly written, it would still be possible to call the function in an explicit loop on a one-element-at-a-time basis, but should rarely be a need.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 12 Aug 2020

Edited:

dpb
on 15 Aug 2020

Community Treasure Hunt

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

Start Hunting!