annotate toolboxes/FullBNT-1.0.7/KPMtools/subv2indKPM.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function ndx = subv2indKPM(siz, subv)
wolffd@0 2 % SUBV2IND Like the built-in sub2ind, but the subscripts are given as row vectors.
wolffd@0 3 % ind = subv2ind(siz,subv)
wolffd@0 4 %
wolffd@0 5 % siz can be a row or column vector of size d.
wolffd@0 6 % subv should be a collection of N row vectors of size d.
wolffd@0 7 % ind will be of size N * 1.
wolffd@0 8 %
wolffd@0 9 % Example:
wolffd@0 10 % subv = [1 1 1;
wolffd@0 11 % 2 1 1;
wolffd@0 12 % ...
wolffd@0 13 % 2 2 2];
wolffd@0 14 % subv2ind([2 2 2], subv) returns [1 2 ... 8]'
wolffd@0 15 % i.e., the leftmost digit toggles fastest.
wolffd@0 16 %
wolffd@0 17 % See also IND2SUBV.
wolffd@0 18
wolffd@0 19
wolffd@0 20 if isempty(subv)
wolffd@0 21 ndx = [];
wolffd@0 22 return;
wolffd@0 23 end
wolffd@0 24
wolffd@0 25 if isempty(siz)
wolffd@0 26 ndx = 1;
wolffd@0 27 return;
wolffd@0 28 end
wolffd@0 29
wolffd@0 30 [ncases ndims] = size(subv);
wolffd@0 31
wolffd@0 32 %if length(siz) ~= ndims
wolffd@0 33 % error('length of subscript vector and sizes must be equal');
wolffd@0 34 %end
wolffd@0 35
wolffd@0 36 if all(siz==2)
wolffd@0 37 %rbits = subv(:,end:-1:1)-1; % read from right to left, convert to 0s/1s
wolffd@0 38 %ndx = bitv2dec(rbits)+1;
wolffd@0 39 twos = pow2(0:ndims-1);
wolffd@0 40 ndx = ((subv-1) * twos(:)) + 1;
wolffd@0 41 %ndx = sum((subv-1) .* twos(ones(ncases,1), :), 2) + 1; % equivalent to matrix * vector
wolffd@0 42 %ndx = sum((subv-1) .* repmat(twos, ncases, 1), 2) + 1; % much slower than ones
wolffd@0 43 %ndx = ndx(:)';
wolffd@0 44 else
wolffd@0 45 %siz = siz(:)';
wolffd@0 46 cp = [1 cumprod(siz(1:end-1))]';
wolffd@0 47 %ndx = ones(ncases, 1);
wolffd@0 48 %for i = 1:ndims
wolffd@0 49 % ndx = ndx + (subv(:,i)-1)*cp(i);
wolffd@0 50 %end
wolffd@0 51 ndx = (subv-1)*cp + 1;
wolffd@0 52 end
wolffd@0 53
wolffd@0 54 %%%%%%%%%%%
wolffd@0 55
wolffd@0 56 function d = bitv2dec(bits)
wolffd@0 57 % BITV2DEC Convert a bit vector to a decimal integer
wolffd@0 58 % d = butv2dec(bits)
wolffd@0 59 %
wolffd@0 60 % This is just like the built-in bin2dec, except the argument is a vector, not a string.
wolffd@0 61 % If bits is an array, each row will be converted.
wolffd@0 62
wolffd@0 63 [m n] = size(bits);
wolffd@0 64 twos = pow2(n-1:-1:0);
wolffd@0 65 d = sum(bits .* twos(ones(m,1),:),2);
wolffd@0 66