I would like replicate a vector N
times to create a matrix with each copy shifted 1 row down. See image (first column is the vector 1 to 5). It would be great if this can be achieved without using for loop.
So far was able to to do this repmat(my_vector, 1, 5)
to create an N x 5 matrix.
Solution Code
This seems to be a fast approach based on
repmat
andbsxfun
as the benchmarks listed in the next section might convince us -Benchmarking
In this section we will cover runtime benchmarking for the various approaches listed on this page for the stated problem.
Benchmarking Code -
Associated function codes (all approaches) -
Runtime plots -
Set #1 [From 10 till 1000 datasizes]:
Set #2 [From 1000 till 10000 datasizes]:
You can do it with
toeplitz
andtril
;or
or if you don't mind some happy flipping: