조회 수: 1(최근 30일)

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 !

dpb
12 Aug 2020

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'}

>>

dpb
14 Aug 2020

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...

dpb
15 Aug 2020

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.

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

Start Hunting!
## 댓글 수: 0

댓글을 달려면 로그인하십시오.