# Reformatting the pattern inside a list

So, in principle, I have a simple problem which goes like this: I have vectors that should consist of lists of 2 numbers, see here:

vector1 = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {1198, 800}, {1199 799}, {1200, 799}, {0, 0}, {0, 0}, {0, 0}};


Unfortunately, sometimes they do not have this structure, but are mixed like in here, where the parenthesis are missing around 1200,800 and the following 0,0:

vector2 = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, 1200, 800, 0, 0, {0, 0}, {0, 0}, {0, 0}};


My current solution is to use While with If inside, to create a new list with the correct structure:

correctVectorPattern[inputVector_] :=
Module[{i = 1, newVector = {}},
While[i <= Length[inputVector],
If[Length[inputVector[[i]]] ==2, {AppendTo[newVector,inputVector[[i]]],i++}, {AppendTo[newVector, {inputVector[[i]], inputVector[[i + 1]]}], i = i + 2}]
];
newVector
]


It gives me what I want:

In[39]:= correctVectorPattern[vector1]
Out[39]= {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {1198, 800}, {1199,799}, {1200, 799}, {0, 0}, {0, 0}, {0, 0}}

In[36]:= correctVectorPattern[vector2]
Out[36]= {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {1200, 800}, {0,0}, {0, 0}, {0, 0}, {0, 0}}


But I can't shake off the feeling that my solution is far from the capabilities of Mma regarding pattern matchin in expressions. Also, I am using If, While and AppendTo, which are probably not the fastest functions for solving my problem. Unfortunately, the vectors are at least 1200 elements long and I need to process 800 of them (these dimensions are due to the size of images I get the vectors from - of 1200 x 800). On my machine it takes me about 14 s, which is not that long, but I would like to scale it up to 5000 x 3000 pixels. Of course Parallel processig or Compiling are also possible and easy options, but with Mma pattern capabilities - shouldn't the solution be something of a 1-liner?

I tried of course myself to solve problem by exploiting patterns, but I end up processing all of the elements in the same way. So I simply don't know how to tell MMa to group elements inside the list that are whithout parenthesises into 2-tuples, while keeping the other 2-tuples..

Does somebody have a more elegant or faster solution to my problem? Thanks a lot!

(And should you wonder why I ask, even though I have working solution: .. a better understanding of Mma doesn't hurt at all ;-) )

Try

 Partition[DeveloperToPackedArray[Flatten[vector2]], 2]


DeveloperToPackedArray guarantees that you have a packed array in the end that is much more efficient to work with.

But it would be more efficient to never let such ragged lists appear in numerical computations.

By the way: Using patterns is usually not a good way to speed up computations: pattern matching takes time that can be saved by guaranteeing that your input can be processed blindly by vectorized routines.

• Wow, that was fast, thanks a lot, that is exactly what I wanted :) But: I get 2 Outputs: ony is a string "ry" and the other is the vector. WHat is the significance of the "ry" and how can I get rid of it? Nov 4, 2018 at 13:01
• Well, it is Sunday and I am lying in wait for a good question here ;) Nov 4, 2018 at 13:02
• Sorry, forget about the ry - I was too enthusiastic typing it in my front end and I acidentally copied the last two letters form your "Try" in front of the expresssion... Nov 4, 2018 at 13:10
• No problem. Actually, I overlooked the "ry". ^^ Nov 4, 2018 at 13:11