view sequences/+seq/slices.m @ 61:eff6bddf82e3 tip

Finally implemented perceptual brightness thing.
author samer
date Sun, 11 Oct 2015 10:20:42 +0100
parents 9e7be347b3a0
children
line wrap: on
line source
% slices - sequence obtained from slices of an array

classdef slices < seq
	properties (GetAccess=private, SetAccess=immutable)
		slice  % natural -> [Size1->A]
		elsz   % [[1,E]]
		length % natural
		dim
	end
	properties (GetAccess=private, SetAccess=private)
		index  % natural
	end
	methods
		function d=slices(x,dim)
			sz=strip1(size(x));
			maxdim=length(sz);
			if nargin<2, dim=maxdim; end
		
			d.elsz=arrset(sz,dim,1); 
			d.slice=slicer(x,dim,maxdim);
			d.length=size(x,dim);
			d.index=1;
			d.dim=dim;
		end

		function s=elsize(o), s=tosize(o.elsz); end
		function s=tostring(o), 
			s=sprintf('slices(<[%s]>,%d)@%d',mat2str(arrset(o.elsz,o.dim,o.length)),o.dim,o.index); 
		end
		function x=head(o), x=o.slice(o.index); end
		function o=next(o) 
			if o.index<o.length, o.index=o.index+1; else o=nil; end
		end
	end
end

function f=slicer(x,dim,maxdim)
	if maxdim>3,
		if dim>=maxdim, f=outslicer(x,dim);
		else f=inslicer(x,dim,maxdim);
		end
	else
		switch dim
			case 1, f=cellget({@(i)x(i), @(i)x(i,:), @(i)x(i,:,:)}, maxdim);
			case 2, if maxdim==3, f=@(i)x(:,i,:); else f=@(i)x(:,i); end
			case 3, f=@(i)x(:,:,i); 
			otherwise, f=outslicer(x,dim); 
		end
	end
end

function f=outslicer(x,dim,maxdim)
	c1=repmat({':'},1,dim-1);
	f=@(i)x(c1{:},i);
end

% for slicing along inner dimensions - need trailing colons to catch remaining
% dimensions
function f=inslicer(x,dim,maxdim)
	c1=repmat({':'},1,dim-1);
	c2=repmat({':'},1,maxdim-dim);
	f=@(i)x(c1{:},i,c2{:});
end