-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathinterleave.m
97 lines (87 loc) · 2.25 KB
/
interleave.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function z = interleave(x,y)
% Interleave 2 vectors.
%
% z = interleave(x,y)
% If the vectors are of different lengths, the remaining elements are
% appended to the end of the resulting vector. x and y can be column or
% row vectors (they need not both be the same), and the output will be
% column or row based on the configuration of x. Interleaving begins with
% the first element of x.
%
% INPUT:
% x: vector to interleave
% y: vector to interleave
%
% OUTPUT:
% z: interleaved vector
%
% EXAMPLE:
% >>interleave([1 2 3 4], [5 6 7 8 9 10])
%
% ans =
%
% 1 5 2 6 3 7 4 8 9 10
%
% AUTHOR:
% Jason Blackaby
%%
[M,N] = size(x);
[P,Q] = size(y);
%Error checking
if(M>1 && N>1)
error('x must be a vector (1xM or Mx1)');
end
if(P>1 && Q>1)
error('y must be a vector (1xM or Mx1)');
end
if(isempty(x) && isempty(y))
z = []; return;
elseif(isempty(x))
z = y; return;
elseif(isempty(y))
z = x; return;
end
%Determine whether x is a column vector or not.
if(M==1)
colvec = false;
else
colvec = true;
end
%Make sure x is longer than y, otherwise, switch them
if(length(x)<length(y))
tmp=x;
x=y;
y=tmp;
swtch = true;
else
swtch = false;
end
%Initialize z
len = length(x) + length(y);
z = zeros(1,len);
%Build indices into z for x and y.
%If the vectors were switched earlier, we still want z to start with
%the first element of x.
if(~swtch)
idy = 2:2:2*length(y);
lasty = idy(end);
idx = 1:2:lasty;
%We know x is longer or the same length as y, so append the rest of
%x at the end.
idx = [idx lasty+1:len];
else
idy = 1:2:2*length(y);
lasty = idy(end);
idx = 2:2:lasty;
%We know x is longer or the same length as y, so append the rest of
%x at the end.
idx = [idx lasty+1:len];
end
%Form z using the indices.
z(idy) = y;
z(idx) = x;
%Make z a column vector if x was
if(colvec)
z=z(:);
end