0

Say I have an array X=[1,2,3,4,5] and I want to duplicate the array twice in the following format:

[1,1,2,2,3,3,4,4,5,5]

What would be the easiest option?

Thanks~

Josh
  • 17
  • 1
  • 10

2 Answers2

4

The kron command does exactly what you need.

X = [1,2,3,4,5];
kron(X, [1 1])
shoelzer
  • 10,648
  • 2
  • 28
  • 49
1

Simply using matrix multiplication:

Y = [1; 1] * X;
Y = Y(:)';

You can do it in one line with RESHAPE function:

Y = reshape([1; 1] * X,1,[]);

Alternatively you can use REPMAT function:

Y = reshape(repmat(X,2,1), 1,[]);
yuk
  • 19,098
  • 13
  • 68
  • 99