Fun with Matlab

I just want to create an 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 with one line of code in MATLAB? I believe that there are several answers. No cycles please.

+5
source share
2 answers

With reshape and repmat

 reshape(repmat([1:6],k,1),1,[]) 

With bsxfun -

 reshape(bsxfun(@plus,[1:6],zeros(k,1)),1,[]) 

On popular request with floor -

 floor(1:1/k:6+(k-1)/k) 
+8
source

Let n = 6; and k = 2; . Here are a few alternatives:

 kron(1:n,ones(1,k)) 

or

 ceil(1/k:1/k:n) 

or

 double(uint64(1:n*k)/k) 
+8
source

Source: https://habr.com/ru/post/1213261/


All Articles