GRREMOVENODES Remove several nodes in a graph. usage: [NODES2 EDGES2] = grRemoveNodes(NODES, EDGES, NODES2REMOVE) remove the nodes with indices NODE2REMOVE from array NODES, and also remove edges containing the nodes NODE2REMOVE. Example nodes = [... 10 10; 20 10; 30 10; ... 10 20; 20 20; 30 20]; edges = [... 1 2; 1 4; 1 5; ... 2 3; 2 5; 2 6; ... 3 6; 4 5; 5 6]; toRemove = [3 4]; [nodes2 edges2] = grRemoveNodes(nodes, edges, toRemove); drawGraph(nodes2, edges2); axis equal; axis([0 40 0 30]); See also grRemoveEdges
0001 function [nodes2, edges2] = grRemoveNodes(nodes, edges, rmNodes) 0002 %GRREMOVENODES Remove several nodes in a graph. 0003 % 0004 % usage: 0005 % [NODES2 EDGES2] = grRemoveNodes(NODES, EDGES, NODES2REMOVE) 0006 % remove the nodes with indices NODE2REMOVE from array NODES, and also 0007 % remove edges containing the nodes NODE2REMOVE. 0008 % 0009 % Example 0010 % nodes = [... 0011 % 10 10; 20 10; 30 10; ... 0012 % 10 20; 20 20; 30 20]; 0013 % edges = [... 0014 % 1 2; 1 4; 1 5; ... 0015 % 2 3; 2 5; 2 6; ... 0016 % 3 6; 4 5; 5 6]; 0017 % toRemove = [3 4]; 0018 % [nodes2 edges2] = grRemoveNodes(nodes, edges, toRemove); 0019 % drawGraph(nodes2, edges2); 0020 % axis equal; axis([0 40 0 30]); 0021 % 0022 % See also 0023 % grRemoveEdges 0024 % 0025 0026 % ------ 0027 % Author: David Legland 0028 % E-mail: david.legland@inrae.fr 0029 % Created: 2003-08-13 0030 % Copyright 2003-2024 INRA - TPV URPOI - BIA IMASTE 0031 0032 %% edges processing 0033 0034 % remove all edges connected to one of the nodes to remove 0035 edges2 = edges(~any(ismember(edges, rmNodes), 2), :); 0036 0037 % change edges information, due to the node index shift 0038 for i = 1:length(rmNodes) 0039 inds = edges2 > (rmNodes(i) - i + 1); 0040 edges2(inds) = edges2(inds) - 1; 0041 end 0042 0043 0044 %% nodes processing 0045 0046 % number of nodes 0047 N = size(nodes, 1); 0048 NR = length(rmNodes); 0049 N2 = N-NR; 0050 0051 % allocate memory 0052 nodes2 = zeros(N2, 2); 0053 0054 % process the first node 0055 nodes2(1:rmNodes(1)-1,:) = nodes(1:rmNodes(1)-1,:); 0056 0057 for i = 2:NR 0058 inds = rmNodes(i-1)+1:rmNodes(i)-1; 0059 if isempty(inds) 0060 continue; 0061 end 0062 nodes2(inds - i + 1, :) = nodes(inds, :); 0063 end 0064 0065 % process the last node 0066 nodes2(rmNodes(NR)-NR+1:N2, :) = nodes(rmNodes(NR)+1:N, :);