function [r,c] = imslice(siz,k)
%IMSLICE Return index into image deck.
%	ELEM = IMSLICE([M N P],K) returns the element positions
%	of the image slice K in an M-by-N-by-P image deck.  IMSLICE
%	is used to extract or insert images into an image deck.  For
%	instance,
%	    D(imslice([m n p],2)) = A;
%	inserts the image A into image slice 2 of the deck D.  Likewise,
%	    A = D(imslice([m n p],3));
%	extracts image slice 3 from the deck D.  More than one image
%	can be extracted at once if K is a vector.  For example,
%	    DNEW = D(imslice([m n p],3:5));
%	extracts the deck DNEW (with 3 images) from the deck D.
%
%	Image decks are a set of images arranged row-wise in a matrix.
%	An image deck containing the 3 images (A1,A2,A3) can be created
%	using D = [A1,A2,A2]; To display an image deck with the SLICE 
%	command, you must first reshape the deck using V = RESHAPE(D,M*N,P).
%	For example:
%	    load mri, d = find(D==1);
%	    V = reshape(D,siz(1)*siz(2),siz(3)); V(d) = NaN*ones(size(d));
%	    slice(V,[],64,13,siz(1)), colormap(map), shading flat
%	displays slices from the MRI data.
%
%	See also IMMOVIE, MONTAGE, SLICE.

%	Clay M. Thompson 5-12-93
%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.7 $  $Date: 1993/08/26 19:34:13 $

error(nargchk(2,2,nargin));

if any( (k<0) | (k>siz(3)) ), error('Index K out of range.'); end
i = 1:siz(1); j = 1:siz(2);

if nargout==2
  [kk,jj] = meshgrid(k,j);
  r = i';
  c = jj + (kk-1)*siz(2);
  c = c(:);

else
  [jj,ii,kk] = meshgrid(j,i,k);
  r = reshape(ii + (jj-1)*siz(1) + (kk-1)*prod(siz(1:2)), ...
              siz(1),siz(2)*length(k));
  
end




