To expand on this concept further, I generally avoid using augment/stack functions with vectors of indeterminate size. The reason being that there is generally quite a bit of overhead in using those functions when it comes to execution time. I propose an alternative method shown here. Firstly we perform the test on the input matrix to determine the size of the result vector and location of results in input matrix. Then we instantiate the result vector and fill it in. To demonstrate the difference in execution I've also created a 100x100 input vector and timed the execution of both algorithms. The execution time difference becomes more apparent the larger the input matrix becomes.