% interleave - interleave elements of an array
%
% interleave :: [[1,N]], [[1,M]] -> [[1,N+M]].
%
% interleave(A,B) interleaves the elements of A and B until there are
% no elements left in one of them. The remaining elements of the other
% are then appending. 
function z=interleave(x,y)

x=x(:);
y=y(:);

nx=length(x);
ny=length(y);

if nx>ny
	z(1:2:2*ny-1)=x(1:ny);
	z(2:2:2*ny)=y;
	z(2*ny+1:nx+ny)=x(ny+1:end);
else
	z(1:2:2*nx-1)=x;
	z(2:2:2*nx)=y(1:nx);
	z(2*nx+1:nx+ny)=y(nx+1:end);
end

