Will approach him step by step.
You have an array of d
:
d=[3 2 4 2 2 2 3 5 1 1 2 1 2 2 2 2 2 9 2]
and you apply to it
q = diff([0 d 0] == 2);
This takes the derivative of [0 d 0] == 2
. Basically, it takes the derivative of all numbers that are 2. The result of [0 d 0] == 2
is:
0 0 1 0 1 1 1 0 0 0 0 1 0 1 1 1 1 1 0 1 0
You can see that there is 1 when there are 2 in the original vector, and it has 0 in begging and 0 at the end. If we take the derivative of q = diff([0 d 0] == 2)
:
q = 0 1 -1 1 0 0 -1 0 0 0 1 -1 1 0 0 0 0 -1 1 -1
You get 1 when 2 appears in the original vector and -1 when it disappears. The last line finds only 1 and only -1 separately and subtracts the indices where they appeared, so you can now how many numbers are between them. Since 1 means โthe number 2 begins,โ and -1 means โno more than 2 lines per line,โ this will give you a series length of 2 lines.
v = find(q == -1) - find(q == 1); v= 1 3 1 5 1
At the beginning there is one single, then there is a series of 3 2s, then another single, then 5 come and 9 are divided, the last comes.
source share