generating distanced matrix for an n-dimensional hypercube

左心房为你撑大大i 提交于 2020-01-14 06:06:13

问题


is there any algorithm or method of generating the adjacency matrix for a hypercube for any dimension? say your input is 5 it would create a 5-dimensional hypercube

all i can find are sources from wiki

and wolfram


回答1:


If you want to generate the vertices of a N-D unit hypercube, you can basically make an N-value truthtable. Here's some code I use for that:

function output = ttable(values)

  output = feval(@(y)feval(@(x)mod(ceil(repmat((1:x(1))', 1, numel(x) - 1) ./ repmat(x(2:end), x(1), 1)) - 1, repmat(fliplr(y), x(1), 1)) + 1, fliplr([1 cumprod(y)])), fliplr(values));
end

and to get the vertices of a 5-D hypercube you can call it like this:

vertices = ttable(ones(1, 5) * 2) - 1;

From here you can calculate the adjacency matrix by finding all vertices that differ by only one bit, i.e.:

adj_list = zeros(2^5, 5);
adj_mat = zeros(2^5, 2^5);
for v=1:2^5
  L1_dists = sum(abs(vertices - repmat(vertices(v, :), 2^5, 1)), 2);
  adj_list(v, :) = find(L1_dists == 1);
  adj_mat(v, find(L1_dists == 1)) = 1;
end


来源:https://stackoverflow.com/questions/10077008/generating-distanced-matrix-for-an-n-dimensional-hypercube

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!