Interweaving vectors

前端 未结 6 1052
傲寒
傲寒 2020-12-06 01:00

I would like to interweave two vectors in MATLAB. In fact, I\'d actually just like to add a zero between each element, but I figured I\'d ask the question in such a way that

6条回答
  •  感情败类
    2020-12-06 01:31

    For the most simple case there's a quite "elegant" solution with a one-liner as described in this answer to a similar question.

    a = [9 8 7];
    b = [1 2 3];
    output = kron(a, [1 0]) + kron(b, [0 1]);
    

    As stated in the answer be aware that this is less efficient however might serve for a more general purpose than just interleaving as it uses the Kronecker product that is well-defined between two matrices.

提交回复
热议问题