Interleave and Deinterleave a vector into two new vectors

前端 未结 4 783
遇见更好的自我
遇见更好的自我 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:40

    Just to add another option, you could use the deal function and some precomputed indices. This is basically the same as the answer from Peter M, but collecting the assignments into single lines:

    X = randi(10, [1 20]);  % Sample data
    ind1 = 1:2:numel(X);    % Indices for x1
    ind2 = 2:2:numel(X);    % Indices for x2
    
    [x1, x2] = deal(X(ind1), X(ind2));  % Unweave (i.e. deinterleave)
    
    [X(ind1), X(ind2)] = deal(x1, x2);  % Interleave
    

提交回复
热议问题