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

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