Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fun with matlab [duplicate]

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.

like image 560
roni Avatar asked Feb 12 '15 17:02

roni


2 Answers

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)
like image 66
Luis Mendo Avatar answered Nov 15 '22 03:11

Luis Mendo


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)
like image 37
Divakar Avatar answered Nov 15 '22 02:11

Divakar