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