I want to just generate a array like this:
a = [1 1 2 2 3 3 4 4 5 5 6 6 ....]
%% or something like this
a = [1 1 1 .. ktimes 2 2 2 ... ktimes .....]
Can this be done by a single line of code in MATLAB ? I believe several answers exist. No loops please.
Let n = 6;
and k = 2;
. Here are some alternatives:
kron(1:n,ones(1,k))
or
ceil(1/k:1/k:n)
or
double(uint64(1:n*k)/k)
With reshape
and repmat
reshape(repmat([1:6],k,1),1,[])
With bsxfun
-
reshape(bsxfun(@plus,[1:6],zeros(k,1)),1,[])
On popular demand with floor
-
floor(1:1/k:6+(k-1)/k)
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