Searching for specific pattern and returning 16 values/data characters from the last index of occurrence of my specific pattern

조회 수: 1 (최근 30일)
Welcome guys !
I'm in a process of 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 includes 0 or 1 values. For instance:
input1=[0 0 0 0 0 1 0 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ]; % it's array of integer values of 0 or 1
I want to search for specific substring which it's given in my case the substring is:
substring=[0 1 0 1] % it's array of integer values of 0 or 1
so I want to search for my giving substring in my input1, and to return at each occurance of my substring in input1 the 16 offset data immediately following my substring from the last index of occurrence of my substring in my input1.
to clarify more, So if I have input1 as I mentioned above, and constant substring= [0 1 0 1] which it's constant vector/array that Im searching for it in my input1. then what I want to do is to find each occurrence of substring in my input1 and return 16 offset data from the last index of occurrence immediately following it.
according to my example the function gets as inputs: (input1, substring) and return a matrix(two dimensional array) that has those outputs:
Output=[0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 ; 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
so first row is represent first occurence of 0101 and the 16 offset data that are immediately following it is: 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
so second row is represent first occurence of 0101 and the 16 offset data that are immediately following it is: 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
number of rows is according to number of occurance of my substring ..so if my substring occured and appeared n times in my input1 , this means my matrix has n rows of the 16 offset data that are immediately following each occurances respectively to each occurance of my substring.
there can't be overlab between first occurance of my substring to next occurance of my substring in my input1, this means there's always at least 16 offset data immediately following each occurance of my substring in my input1.
Could you guys assist and help me to implement that in matlab? thanks alot!
thanks alot for helpers !

답변 (1개)

Walter Roberson
Walter Roberson 2020년 8월 6일
input1 = [0 0 0 0 0 1 0 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ];
substring=[0 1 0 1];
N = 16;
positions = strfind(input1(1:end-N+1), substring);
Output = cell2mat(arrayfun(@(idx) input1(idx+length(substring):idx+length(substring)+N-1), positions, 'uniform', 0 ).');
  댓글 수: 2
Jimmy cho
Jimmy cho 2020년 8월 7일
First appreciated ! and Understood your code.
So what I've did took those substring outputs (Output=[0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 ; 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]; ) , each substring I entered it to another function , the another function it was hard for me to write in matlab but Im still struggling to write it in matlab, so write it in c++ : (the first input is an integer number called crcData -it's integer- , the other input is each substring of my output ) , so I call this function like this:
calcCRC(10, output[1]) , Output[1] is according to my Output two dimensional array = 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 .
Could you please help me how could I do that function down (I wrote it in c++) to write it in matlab? Im struggling with matlab because generally not writting in matlab, but just as @one time I want to use matlab .
thanks alot if you could help and appreciated.
my function that I write in c++ is:
uint16_t calcCRC(uint8_t crcData, uint16_t crcReg)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
if (((crcReg & 0x8000) >> 8) ^ (crcData & 0x80))
{
crcReg = (crcReg << 1) ^ CRC16_POLY;
}
else
{
crcReg = (crcReg << 1);
}
crcData <<= 1;
}
return crcReg;
}

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by