Find graph node names beginning with a few characters

2 vues (au cours des 30 derniers jours)
Kamal Premaratne
Kamal Premaratne le 7 Oct 2021
Is there a way to find out all the node names of a graph that begins with a few characters?
For example, I am working with the CElegans digraph (wormwiring.org). It has nodes named 'DA01', 'DA02', etc. I want to know all the node names that begin with the characters 'DA'.
Thank you so much.
Kamal

Réponse acceptée

Steven Lord
Steven Lord le 7 Oct 2021
Using a modified version of one of the documentation examples and splitting the code into multiple lines for ease of explanation.
A = triu(magic(4));
names = {'alpha' 'beta' 'gamma' 'alphabet'};
G = graph(A,names,'upper','omitselfloops')
G =
graph with properties: Edges: [6×2 table] Nodes: [4×1 table]
listOfNames = G.Nodes.Name
listOfNames = 4×1 cell array
{'alpha' } {'beta' } {'gamma' } {'alphabet'}
whichNodesStartWithA = startsWith(listOfNames, 'a')
whichNodesStartWithA = 4×1 logical array
1 0 0 1
namesStartingWithA = listOfNames(whichNodesStartWithA)
namesStartingWithA = 2×1 cell array
{'alpha' } {'alphabet'}
whichNodesAreTheyInG = findnode(G, namesStartingWithA)
whichNodesAreTheyInG = 2×1
1 4
  1 commentaire
Kamal Premaratne
Kamal Premaratne le 8 Oct 2021
Fantastic. I had no idea of this function. Just found out about "endsWith", "contains", "pattern", and "lettersPattern".
Thank you so much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by