-1

It's hard to explain so I will show an example of what I would like to do:

x = [1 2 3 4 5]

I would like the outcome to be:

x = [1 1 2 2 3 3 4 4 5 5]

Preferably without the use of a for loop, but either method would be appreciative.

Thanks.

Josh Beckwith
  • 1,432
  • 3
  • 20
  • 38

2 Answers2

4

You can also use the Kronecker tensor product (kron function) which is pretty neat:

x = kron(x,ones(1,2))

x =

     1     1     2     2     3     3     4     4     5     5
Benoit_11
  • 13,905
  • 2
  • 24
  • 35
1

If you want it sorted as you have here, you could do:

y = sort([x x]);

alternatively if the order matters:

y = reshape([x;x],[1,2*length(x)])
craigim
  • 3,884
  • 1
  • 22
  • 42