Interleave and Deinterleave a vector into two new vectors

前端 未结 4 684
遇见更好的自我
遇见更好的自我 2021-01-16 08:17

Interleaver: Assume we have vector X= randi(1,N) I would like to split the contents of X into two new vectors X1and <

4条回答
  •  抹茶落季
    2021-01-16 08:51

    I think the terminology in this question is reversed. Interleaving would be to merge two vectors alternating their values:

    x1 = 10:10:100;
    x2 = 1:1:10;
    x = [x1;x2];
    x = x(:).';
    

    This is the same as the one-liner:

    x = reshape([x1;x2],[],1).';
    

    Deinterleaving would be to separate the interleaved data, as already suggested by David in a comment and Tom in an answer:

    y1 = x(1:2:end);
    y2 = x(2:2:end);
    

    but can also be done in many other ways, for example inverting the process we followed above:

    y = reshape(x,2,[]);
    y1 = y(1,:);
    y2 = y(2,:);
    

    To verify:

    isequal(x1,y1)
    isequal(x2,y2)
    

提交回复
热议问题