So8res
So8res

Reputation: 10356

Splice matlab vectors

I have two matlab vectors. The first has N elements, the other has k*N. I know what k is, and I want to splice the lists such that each element from the first vector appears before the corresponding k elements from the next vector. For example:

k = 3
x = [1 5 9]
y = [2 3 4 6 7 8 10 11 12]

should be combined to look like this:

z = [1 2 3 4 5 6 7 8 9 10 11 12]

Is there an easy way to do this quickly? My x's and y's are pretty big. Thanks!

Upvotes: 4

Views: 3683

Answers (1)

Jonas
Jonas

Reputation: 74930

You can do this via some reshaping

k = 3
x = [1 5 9]
y = [2 3 4 6 7 8 10 11 12]

%# make a k-by-n array
z = reshape(y,k,[]);

%# catenate with x
z = [x;z];

%# reorder
z = z(:)'

Upvotes: 7

Related Questions