view general/arrutils/interleave.m @ 13:03694e5c8365

Reorganised some high order list functions to correct class-based method dispatch; fixed some docs.
author samer
date Wed, 16 Jan 2013 12:12:34 +0000
parents e44f49929e56
children
line wrap: on
line source
% 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