pdist2 with wrapping

조회 수: 2 (최근 30일)
Tejas
Tejas 2021년 1월 21일
댓글: Rik 2021년 1월 22일
I have a square area of gridpoints and I'm trying to create a graph where each point is joined to its neighbouring points based on a Chebychev distance. From an earlier question here, I have the following methodology:
M = 50; N = 50;
[I,J] = ndgrid(1:M,1:N);
IJ = [I(:),J(:)];
[W,K] = pdist2(IJ,IJ,'chebychev','Smallest',9);
From here, columns of W containing only 1 can be selected and can be matched with columns of K to get the adjacency matrix. In this case however, I want to create a graph where the edges of the square are wrapped, that is, the leftmost (or topmost) points are also connected to the rightmost (or bottommost) points. How can I add this condition in pdist2?

답변 (2개)

Rik
Rik 2021년 1월 21일
You could maybe modify the internal code of pdist2, but you should not do that. The easiest way here is to just create copies of your array with repmat. Don't forget to remove the padding from the result as well.
  댓글 수: 4
Tejas
Tejas 2021년 1월 22일
편집: Tejas 2021년 1월 22일
Wouldn't that be very inefficient? I think the manual method takes far lesser time. I already have a method to create a rectangular graph where the gridpoints are connected vertically, horizontally, and diagonally. I only want to ensure the edges are connected as well, so that every point in the graph will have 8 nearest neighbours at a (Chebychev) distance of 1.
Rik
Rik 2021년 1월 22일
I never claimed this would be efficient. It would work, that is all.

댓글을 달려면 로그인하십시오.


Tejas
Tejas 2021년 1월 22일
I have the following manual (and rugged) method for this question, while someone formulates an elegant and efficient solution. The graph (G) is created using a method given here. This graph is an MxN rectangular grid where the gridpoints are connected vertically, horizontally and diagonally to their neighbouring gridpoints. I use the Chebychev distance, so that the diagonal distances are also 1 instead of sqrt(2). The following code connects the points on the leftmost (or topmost) edges to rightmost (or bottommost) edge. This implies every node has eight nearest nodes at a distance of 1.
% Given graph G, dimensions M horizontally and N vertically
xe = M; ye = N;
s = repelem(sub2ind(p.grid,[xe*ones(1,ye),1:xe],...
[1:ye,ye*ones(1,xe)]),3);
t = zeros(1,3*ye); j = 4;
t(1) = ye; t(2) = 1; t(3) = 2;
t(end-2) = ye-1; t(end-1) = ye; t(end) = 1;
for i = 1:ye-2
t(j) = i; t(j+1) = i+1; t(j+2) = i+2;
j = j + 3;
end
t1 = sub2ind([M,N],ones(1,length(t)),t);
t = zeros(1,3*xe); j = 4;
t(1) = xe; t(2) = 1; t(3) = 2;
t(end-2) = xe-1; t(end-1) = xe; t(end) = 1;
for i = 1:xe-2
t(j) = i; t(j+1) = i+1; t(j+2) = i+2;
j = j + 3;
end
t2 = sub2ind([M,N],t,ones(1,length(t)));
t = [t1,t2];
idx = find(ismember([s',t'],[t',s'],'rows'),1); s(idx)=[]; t(idx)=[];
st = unique([s;t],'rows');
G = addedge(G,st(1,:),st(2,:),ones(1,length(st)));

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by