How can I merge two duplicates of an array?

44 Views Asked by At

If I have an array A = [1 2 3 4 5 6 7 8 9], is there any Matlab command that transforms that array to A = [1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9]?

Or do I need to use algorithms for this?

2

There are 2 best solutions below

2
On BEST ANSWER

Here is the answer.

$$A = [A;A](:)$$

Easy!

0
On

See the documentation for the reshape function. Here is how you can use it:

First, make a $2$-by-$n$ matrix which has both rows equal to $A$ with:

[A;A]

Then use the reshape function to "flatten" this matrix:

reshape([A;A], 1, [])

The output will be what your are looking for.