Frequency of non-increasing and non-decreasing subsequences in Matlab

293 Views Asked by At

Having a sequence of numbers of length L, I need to count how many non-decreasing and non-increasing sub-sequences of exact length are there. For example, if I have a sequence of length 15 $$2, 4, 11,13,3,5,5,6,3,3,2,4,2,14,15$$ I see that non-increasing sub-sequences are $$13,3$$ $$6,3,3,2$$ $$4,2$$ and non-decreasing sub-sequences are $$2,4,11,13$$ $$3,5,5,6$$ $$3,3$$ $$2,4$$ $$2,14,15$$ So here I have

  • 2 non-increasing sub-sequences of length 2
  • 1 non-increasing sub-sequence of length 4
  • 2 non-decreasing sub-sequences of length 2
  • 1 non-decreasing sub-sequences of length 3
  • 2 non-decreasing subs-equence of length 4

Since the maximum length of a non-decreasing (or non-increasing) sub-sequence can be 15 in this case, I thought about representing frequencies through vectors x for non-increasing and y for non-decreasing sub-sequences: $$x=(0,2,0,1,0,0,0,0,0,0,0,0,0,0,0)$$ $$y=(0,2,1,2,0,0,0,0,0,0,0,0,0,0,0)$$

Expanding this to general case of sequence of length L, I wanted to go through the sequence and, using loops, count frequencies of subsequences of the exact lengths. How would I do that? I would create zero-vectors of length L and I would add 1 to the l-th element of zero matrix every time I meet a sub-sequence of length l.

Since my sequence will be of length of few thousands, I wouldn't ask Matlab to write them, but I would ask it to write me particular frequency.

Is this a good approach? Is there some function in Matlab that is doing this?

I will appreciate any help! Thank you!