I want to repeat each row n times, for example:
A= [123
456
789];
so I want to have:
b=[123
123
123
456
456
456
789
789
789];
I have tried repmat:
B = repmat(A,3,1)
But this does not result in the b above...How do I do this?
Rody has provided you with the repmat solution (+1), but I thought it was also worth pointing out that:
A = [123;456;789];
A = ones(N, 1) * A';
A = A(:);
will be close to an order of magnitude faster, since repmat isn't a particularly efficient function. A quick speed test over 10000 iterations yields:
Elapsed time is 0.206894 seconds %#repmat solution
Elapsed time is 0.024718 seconds %#My solution
A final point, I notice in the comments that @Maroun85 suggests using linear indexing. However, I can't see a clever way to construct the required index without a call to repmat, which brings us back to the original source of slow-down. Someone else might be able to come up with a clever way to construct the required index vector though.
EDIT: Rody has updated his answer to provide the aforementioned "clever way". :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With