comparison toolboxes/FullBNT-1.0.7/KPMtools/ind2subvKPM.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 sub = ind2subvKPM(siz, ndx)
2 % IND2SUBV Like the built-in ind2sub, but returns the answer as a row vector.
3 % sub = ind2subv(siz, ndx)
4 %
5 % siz and ndx can be row or column vectors.
6 % sub will be of size length(ndx) * length(siz).
7 %
8 % Example
9 % ind2subv([2 2 2], 1:8) returns
10 % [1 1 1
11 % 2 1 1
12 % ...
13 % 2 2 2]
14 % That is, the leftmost digit toggle fastest.
15 %
16 % See also SUBV2IND
17
18 n = length(siz);
19
20
21 if n==0
22 sub = ndx;
23 return;
24 end
25
26 if all(siz==2)
27 sub = dec2bitv(ndx-1, n);
28 sub = sub(:,n:-1:1)+1;
29 return;
30 end
31
32 cp = [1 cumprod(siz(:)')];
33 ndx = ndx(:) - 1;
34 sub = zeros(length(ndx), n);
35 for i = n:-1:1 % i'th digit
36 sub(:,i) = floor(ndx/cp(i))+1;
37 ndx = rem(ndx,cp(i));
38 end
39
40
41
42 %%%%%%%%%%
43
44 function bits = dec2bitv(d,n)
45 % DEC2BITV Convert a decimal integer to a bit vector.
46 % bits = dec2bitv(d,n) is just like the built-in dec2bin, except the answer is a vector, not a string.
47 % n is an optional minimum length on the bit vector.
48 % If d is a vector, each row of the output array will be a bit vector.
49
50
51 if (nargin<2)
52 n=1; % Need at least one digit even for 0.
53 end
54 d = d(:);
55
56 [f,e]=log2(max(d)); % How many digits do we need to represent the numbers?
57 bits=rem(floor(d*pow2(1-max(n,e):0)),2);