If like me you have no idea what a Kronecker tensor product is you might be interested in this more intuitive (and actually I think faster) solution:
c(ceil((1:length(c)*n)/n));
so here I used vector indexing to replicate the matrix. For example using the two case you have above we could do:
c = 1:3;
c([1 1 1 2 2 2 3 3 3]) %for each
c([1 2 3 1 2 3 1 2 3]) %for times
so the questions is how do we make a vector [1 2 3 1 2 3 1 2 3] without the very functionality you are requesting. So I made a vector with the number of elements we need i.e. 1:9 and then divide by three and round up (i.e. try ceil((1:9)/3)
in the command line.
A bit of benchmarking (I know this stuff should be in loops so maybe this isn't so accurate):
c = 1:3; n = 3;
tic; k = kron(c, ones(1, n)); toc; % 0.000208 seconds.
tic; a = c(ceil((1:length(c)*n)/n)); toc; % 0.000025 seconds.
clear;
c = 1:1000000; n = 3;
tic; k = kron(c, ones(1, n)); toc; % 0.143747 seconds.
tic; a = c(ceil((1:length(c)*n)/n)); toc; % 0.090956 seconds.
clear;
c = 1:10000; n = 1000;
tic; k = kron(c, ones(1, n)); toc; % 0.583336 seconds.
tic; a = c(ceil((1:length(c)*n)/n)); toc; % 0.237878 seconds.
each
option forrep
; been using a hacky one-liner all this time... – Sieverepelem
– Padding