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