s = [1 2 2 4 5 4 8 7 7 8 13];
t = [3 3 11 5 6 6 7 10 12 12 9];
G = graph(s,t,[],compose('%d',1:13));
group = conncomp(G,'OutputForm','cell');
subGraph = cellfun(@(x) subgraph(G,x),group,'UniformOutput',false);
C_out = cell(1,numel(subGraph));
for kk1 = 1:numel(subGraph)
G = subGraph{kk1};
nodes = G.Nodes.Name';
c = cell(1,numel(nodes));
for kk2 = 1:numel(nodes)
nextNodes = neighbors(G,nodes{kk2})';
nonNextNodes = setdiff(nodes,[nodes(kk2),nextNodes]);
if isempty(nonNextNodes)
c{kk2} = str2double(nodes(kk2));
else
[~,pt] = sort(str2double(nonNextNodes));
c{kk2} = str2double(nonNextNodes(pt));
end
end
C_out{kk1} = c;
end
Best Answer