annotate util/ksvd utils/sampgrid.m @ 174:dc2f0fa21310 danieleb

multiple trials with error bars
author Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk>
date Thu, 17 Nov 2011 11:16:15 +0000
parents c3eca463202d
children
rev   line source
idamnjanovic@70 1 function y = sampgrid(x,blocksize,varargin)
idamnjanovic@70 2 %SAMPGRID Sample a multi-dimensional matrix on a regular grid.
idamnjanovic@70 3 % Y = SAMPGRID(X,BLOCKSIZE,I1,I2,...,Ip) extracts block samples of size
idamnjanovic@70 4 % BLOCKSIZE from the p-dimensional matrix X, arranging the samples as the
idamnjanovic@70 5 % column vectors of the matrix Y. The locations of the (1,1,..,1)-th
idamnjanovic@70 6 % elements of each block are given in the index vectors I1,I2,..Ip. The
idamnjanovic@70 7 % total number of samples taken is length(I1)xlength(I2)x...xlength(Ip).
idamnjanovic@70 8 % BLOCKSIZE should either be a p-element vector of the form [N1,N2,...Np],
idamnjanovic@70 9 % or a scalar N which is shorthand for the square block size [N N ... N].
idamnjanovic@70 10 %
idamnjanovic@70 11 % Example: Sample a set of blocks uniformly from a 2D image.
idamnjanovic@70 12 %
idamnjanovic@70 13 % n = 512; blocknum = 20000; blocksize = [8 8];
idamnjanovic@70 14 % im = rand(n,n);
idamnjanovic@70 15 % [i1,i2] = reggrid(size(im)-blocksize+1, blocknum);
idamnjanovic@70 16 % blocks = sampgrid(im, blocksize, i1, i2);
idamnjanovic@70 17 %
idamnjanovic@70 18 % See also REGGRID.
idamnjanovic@70 19
idamnjanovic@70 20 % Ron Rubinstein
idamnjanovic@70 21 % Computer Science Department
idamnjanovic@70 22 % Technion, Haifa 32000 Israel
idamnjanovic@70 23 % ronrubin@cs
idamnjanovic@70 24 %
idamnjanovic@70 25 % November 2007
idamnjanovic@70 26
idamnjanovic@70 27
idamnjanovic@70 28 p = ndims(x);
idamnjanovic@70 29 if (p==2 && any(size(x)==1) && length(blocksize)==1)
idamnjanovic@70 30 p = 1;
idamnjanovic@70 31 end
idamnjanovic@70 32
idamnjanovic@70 33 if (numel(blocksize)==1)
idamnjanovic@70 34 blocksize = ones(1,p)*blocksize;
idamnjanovic@70 35 end
idamnjanovic@70 36
idamnjanovic@70 37 n = zeros(1,p);
idamnjanovic@70 38 for i = 1:p
idamnjanovic@70 39 n(i) = length(varargin{i});
idamnjanovic@70 40 end
idamnjanovic@70 41
idamnjanovic@70 42 nsamps = prod(n);
idamnjanovic@70 43
idamnjanovic@70 44 % create y of the same class as x
idamnjanovic@70 45 y = zeros(prod(blocksize),nsamps,class(x));
idamnjanovic@70 46
idamnjanovic@70 47 % ids() contains the index of the current block in I1..Ip
idamnjanovic@70 48 ids = ones(p,1);
idamnjanovic@70 49
idamnjanovic@70 50 % block_ids contains the indices of the current block in X
idamnjanovic@70 51 block_ids = cell(p,1);
idamnjanovic@70 52 for j = 1:p
idamnjanovic@70 53 block_ids{j} = varargin{j}(1) : varargin{j}(1)+blocksize(j)-1;
idamnjanovic@70 54 end
idamnjanovic@70 55
idamnjanovic@70 56 for k = 1:nsamps
idamnjanovic@70 57 block = x(block_ids{:});
idamnjanovic@70 58 y(:,k) = block(:);
idamnjanovic@70 59
idamnjanovic@70 60 % increment ids() and block_ids{}
idamnjanovic@70 61 if (k<nsamps)
idamnjanovic@70 62 j = 1;
idamnjanovic@70 63 while (ids(j) == n(j))
idamnjanovic@70 64 ids(j) = 1;
idamnjanovic@70 65 block_ids{j} = varargin{j}(1) : varargin{j}(1)+blocksize(j)-1;
idamnjanovic@70 66 j = j+1;
idamnjanovic@70 67 end
idamnjanovic@70 68 ids(j) = ids(j)+1;
idamnjanovic@70 69 block_ids{j} = varargin{j}(ids(j)) : varargin{j}(ids(j))+blocksize(j)-1;
idamnjanovic@70 70 end
idamnjanovic@70 71 end
idamnjanovic@70 72