필터 지우기
필터 지우기

Sorting a list of edges

조회 수: 5 (최근 30일)
fabio freschi
fabio freschi 2014년 1월 13일
답변: maryline bawedin 2016년 10월 27일
Hi everybody, I have a list of edges defined as connectivity matrix, that defines a closed loop. For example:
edg = [1 4; 7 4; 8 2; 7 5; 3 8; 1 6; 5 2; 9 6; 3 9 ];
defines a closed loop made of 9 nodes numbered from 1 to 9. These edges are neither sorted nor equi-oriented. Now I would like to sort the edge list starting from the first edge to have an adjacent list of edges. In other words the desired output is
edg_sort = [1 4; 4 7; 7 5; 5 2; 2 8; 8 3; 3 9; 9 6; 6 1];
I was able to find a solution using a for-loop
% preallocate
edg_sort = zeros(size(edg));
% first edge
edg_sort(1,:) = edg(1,:);
% list of visited edges
edg_visited = zeros(size(edg,1),1);
edg_visited(1) = 1;
for i = 2:size(edg,1)
% node to search for
inod = edg_sort(i-1,2);
% indices of edges available
iedg = find(edg_visited == 0);
% find node (in local row indexing)
[idx,jdx] = find(edg(iedg,:) == inod);
% load new edge: first node
edg_sort(i,1) = inod;
% load new edge: second node
if jdx == 1
edg_sort(i,2) = edg(iedg(idx),2);
else
edg_sort(i,2) = edg(iedg(idx),1);
end
% update list of visited edges
edg_visited(iedg(idx)) = 1;
end
but I wonder whether it is possible to avoid the for-loop, maybe using the adjacency matrix or a similar structure. Any help is appreciated!
Thanks
Fabio

답변 (1개)

maryline bawedin
maryline bawedin 2016년 10월 27일
Hello...Late answer for this post. Here is what I did, a bit shorter code... Below, the variable "RegionEdgeIndex" (1st line) can be your "edg" list
RegionLeftEdgeIndexSorted= sortrows(RegionEdgeIndex, 1);
RegionLeftEdgeIndexSortedTemp= RegionLeftEdgeIndexSorted;
k=1;
RegionEdgeSorted(k,:)= RegionLeftEdgeIndexSortedTemp(1,:); %initialization
RegionLeftEdgeIndexSortedTemp(1,:)=[];
row=1;
while k<=length(RegionEdgeReferenceList)-1
[row,col] = find(RegionLeftEdgeIndexSortedTemp(:,1)== RegionEdgeSorted(k,2) );
k=k+1 ;
RegionEdgeSorted(k,:)=RegionLeftEdgeIndexSortedTemp(row,:)
RegionLeftEdgeIndexSortedTemp(row,:)=[];
end

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by