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