0001 function [out] = idv2mdv(n, fragment)
0002
0003
0004
0005
0006 global pseudohash1 pseudohash2
0007
0008 if isempty(pseudohash1)
0009 pseudohash1 = sparse(15, 2048);
0010 pseudohash2 = {};
0011 end
0012
0013 if nargin == 1
0014 out = sparse(1);
0015 for i = 1:n
0016 out = [[out;sparse(1, size(out,2))] [sparse(1, size(out,2));out]];
0017 end
0018 return;
0019 end
0020
0021 if length(fragment) ~= n
0022 display('error in fragment length');
0023 return;
0024 end
0025
0026 ncarbons = sum(fragment)+1;
0027 out = sparse(ncarbons, 2^n);
0028 fragment = fragment(n:-1:1);
0029
0030 m = memoize(n, fragment);
0031 if ~isempty(m)
0032 out = m;
0033 return;
0034 end
0035 for i = 0:(2^n-1)
0036 t = dec2bin(i,n);
0037 t(logical(fragment));
0038 i2 = sum(t(logical(fragment)) == '1');
0039 out(i2+1,i+1) = 1;
0040 end
0041 memoize(n, fragment, out);
0042 return
0043
0044 function [matrix] = memoize(n, fragment, matrix)
0045 global pseudohash1 pseudohash2
0046 fragmentindex = 0;
0047 for i = 1:length(fragment)
0048 fragmentindex = fragmentindex*2;
0049 fragmentindex = fragment(i) + fragmentindex;
0050 end
0051
0052 tindex = pseudohash1(n, fragmentindex);
0053 if tindex == 0
0054 if nargin < 3
0055 matrix = [];
0056 return;
0057 else
0058 tindex = length(pseudohash2)+1;
0059 pseudohash1(n, fragmentindex) = tindex;
0060 pseudohash2{tindex} = matrix;
0061 end
0062 else
0063 matrix = pseudohash2{tindex};
0064 return;
0065 end
0066 return
0067