1
我有一個邊緣列表,如下所示。如何在Matlab中從邊緣列表中找到連接的邊線
75 77
77 78
78 79
60 63
61 65
65 67
57 62
62 64
67 81
81 85
我想分開連接的邊如下。
75 60 61 57
77 63 65 62
78 67 64
79 81
85
我在Matlab中編寫了下面的代碼。
edges = [75 77; 77 78; 78 79; 60 63; 61 65; 65 67; 57 62; 62 64; 67 81; 81 85];
visited = zeros(size(edges,1),1);
cEdges = [];
cEdgesC = 1;
cEdges(1,cEdgesC) = edges(1,1);
cEdges(2,cEdgesC) = edges(1,2);
visited(1,1) = 1;
orgR = 1;
orgC = 2;
while sum(visited) <= size(visited,1)
cEdgesR = nnz(cEdges(:,cEdgesC));
currentVertex = cEdges(cEdgesR,cEdgesC);
fprintf('\n%d\t%d',cEdgesR,currentVertex);
[foundR,foundC] = find(edges==currentVertex);
foundR(foundR==orgR) = [];
foundC(foundC==orgC) = [];
while isempty(foundR)
fmt=repmat('%d ',1,cEdgesR);
fprintf('\nLoop found: ');
fprintf(fmt,(cEdges(:,cEdgesC))');
cEdgesC = cEdgesC+1;
orgR = find(visited==0, 1, 'first');
orgC = 1;
cEdges(1,cEdgesC) = edges(orgR,orgC);
cEdges(2,cEdgesC) = edges(orgR,orgC+1);
visited(orgR,1) = 1;
cEdgesR = nnz(cEdges(:,cEdgesC));
currentVertex = cEdges(2,cEdgesC);
fprintf('\n%d\t%d',cEdgesR,currentVertex);
[foundR,foundC]=find(edges==currentVertex);
foundR(foundR==orgR) = [];
foundC(foundC==orgC) = [];
if isempty(foundR)
// What to do here?
end
end
fprintf('\t%d\t%d',foundR,foundC);
orgR = foundR;
if foundC == 1
cEdges(cEdgesR+1,1) = edges(foundR,foundC+1);
orgC = foundC+1;
else
cEdges(cEdgesR+1,1) = edges(foundR,foundC-1);
orgC = foundC-1;
end
visited(foundR,1) = 1;
end
該代碼以不停止的方式在循環中運行。我覺得問題是在內循環結束時。如果再次遇到同樣的情況,我該如何跳回內循環的開始?謝謝。
編輯: Example larger matrix of edges。
它適用於我發佈的示例輸入。我嘗試了一個更大的矩陣,將其附加到問題。它沒有正確分離所有鏈接的邊緣。謝謝。 –