将向量交织和解交织成两个新向量 [英] Interleave and Deinterleave a vector into two new vectors
问题描述
交织器:假设我们有向量X= randi(1,N)
,我想将X
的内容分成两个新的向量X1
和X2
,以使X
的第一个元素是X1
的第一个元素,X2
的第一个元素是X
的第二个元素,X
的第三个元素是X1
的第二个元素,而X
的第四个元素是X2
...的第二个元素,依此类推,直到向量X的最后一个元素.
Interleaver: Assume we have vector X= randi(1,N)
I would like to split the contents of X
into two new vectors X1
and X2
such that the first element of X
is the first element of X1
, the first element of X2
is the second element of X
, the third element of X
is the second element of X1
and the fourth element of X
is the second element of X2
... etc till the last element of the vector `X.
我有以下想法
X1(1)=X(1);
X2(1)=X(2);
for i=1:length(X)
X1(i)= X(i+2);
end
for j=2:length (X)
X2(i)= X(i+2)
end
我的问题是:我的方法正确吗?有更好的方法吗?
My question is: is my method correct? is there a better way to do it?
解交织器
我也有相反的问题,所以基本上在这种情况下,我有X1
和X2
并想恢复X
,我将如何有效地恢复X
?
Deinterleaver
I also have the reverse problem so basically in this case I have X1
and X2
and would like to recover X
, how would I efficiently recover X
?
推荐答案
我认为这个问题中的术语是相反的.交织是合并两个向量,交替改变它们的值:
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(:).';
这与单线纸一样:
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,:);
要验证:
isequal(x1,y1)
isequal(x2,y2)
这篇关于将向量交织和解交织成两个新向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!