Split vector by zeros

90 Views Asked by At

I have got a problem with splitting a vector by zeros.

I have a vector for example $$v=[1\ 3\ 2\ 6\ 4\ 0\ 0\ 2\ 4\ 6\ 0\ 0\ 0\ 3\ 1]$$

I need to get vectors like $$v_1=[1\ 3\ 2\ 6\ 4]$$

$$v_2=[2\ 4\ 6]$$

$$v_3=[3\ 1]$$

Is there any way to do this?

Of course I don't know of how many vectors are included in main vector $v$ and how many zeros delimits vectors.

1

There are 1 best solutions below

2
On

It looks like you're using zero blocks of any length as delimiters. A simple way to do this using computer code would skip any initial zeros, then keep track of the nonzero terms so far, building a temporary vector of increasing length, and when/if another zero is encountered that temp vector gets "split off" and you start again. Of course if you are on a nonzero entry at the "end of file" that final temp vector gets split off as the last one.

I don't know of any common mathematical construct which would do the splitting you want.