Find position of cell array within another cell array or table or in a structure
68 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Timothy
el 17 de Dic. de 2025 a las 15:28
Comentada: Fangjun Jiang
hace alrededor de 2 horas
I have a number of nodes on a graph and a number of edges that connect them. Attached to each edge is a structure wear the first field is the EndNodes data from the edge table which defines the edges. I want to search the struct array to find the structure that matches the selected edge EndNodes but I can't figure out the syntax. The closest I've come is something like this where EdgeData is the struct array and data is the 2x1 cell containing the desired EndNodes:
selectedEdge=EdgeData(ismember(data,array2table({app.EdgeData.EndNodes}).Var1))
This fails because the inputs of ismember must be character arrays rather than cell arrays containing character arrays. Concatenating them could work but I would then simply be detecting if the two names of the edges are present and not whether they are contained together within one field. I've tried ==, ismember, strcmp, isequal and I've run out of things to try and ideas of documentation to comb through. It's possible that arrayfun might be the way forward but I don't really understand it honestly
PS yes I'm using app designer hence the referencing of EdgeData as a property
2 comentarios
Fangjun Jiang
el 17 de Dic. de 2025 a las 15:53
Providing an example of the data would be most helpful to understand the need. You can construct a simplifed data using code.
Respuesta aceptada
Fangjun Jiang
el 17 de Dic. de 2025 a las 17:05
Something like this?
data={{'Littleport'},{'West River'}};
EndNodes={{'Bigport'},{'West River'};
{'Littleport'},{'West River'};
{'Littleport'},{'East River'}};
Index=ismember(string(EndNodes),string(data),'row')
5 comentarios
Fangjun Jiang
hace alrededor de 2 horas
The lesson learned here is to provide an "EXACT" example inputs and the expected outputs for the most efficient Q&A. Imaging if you had provided the correct "data" and "EndNodes" initially, there would be no need for this lengthy discussion. You don't even need to mention your specific field like "graph" or "edge".
You can use the "Insert a line of code" button and then "RUN" it to provide a valid example data. Note that the example you previously provided in your comment text is not valid.
data={{'Littleport'}{'West River'}}
Más respuestas (1)
Steven Lord
hace alrededor de 23 horas
Are you using a graph or digraph object? Do you have the additional struct information stored as a separate variable or have you added that information as custom attributes in the Edges and/or Nodes properties of the graph/digraph object? I recommend the latter if you're using the object.
If you have that information as custom attributes of a graph or digraph object, to locate the data for a specific edge given the end nodes use the findedge function. Then use that edge index to index into the Edges property.
s = [1 1 2 2 3];
t = [2 4 3 4 4];
G = digraph(s,t);
G.Edges.Weight = [10 20 30 40 50]';
disp(G.Edges)
whichEdge = findedge(G, 2, 3)
theWeight = G.Edges{whichEdge, 'Weight'}
Note that if you've constructed the digraph using a possibly unordered set of source and target indices, and the additional edge information is stored in a separate variable, the order may be different. The EndNodes list order is not guaranteed to be the same as the order in which the edges were specified when the graph/digraph object was created.
s2 = [1 1 3 2 2];
t2 = [2 4 4 3 4];
w2 = [10 20 50 30 40];
D = digraph(s2, t2, w2);
D.Edges
The third edge in the Edges property is not the one specified by the third elements in s2, t2, and w2. So if you were to findedge and use that to index into w2, you'd get the wrong answer. That's one reason I recommend storing both the edge information and the additional attributes inside the graph/digraph object.
E = findedge(D, 2, 3) % Correct edge located
weight = w2(E) % but w2 is in a different order than Weight so wrongly returns 50
weight = D.Edges{E, "Weight"} % Correctly returns weight of 30
5 comentarios
Steven Lord
hace alrededor de 2 horas
What's in the cell? Is it text data? If so there are lots of functions for searching for data in cell arrays containing text.
C = {'apple', 'banana', 'cherry', 'durian'}
namesContainingTheLetterA = contains(C, 'a')
C(namesContainingTheLetterA)
namesWith6Letters = strlength(C) == 6
C(namesWith6Letters)
startsWithB = startsWith(C, 'b')
C(startsWithB)
The set functions like ismember may also be of use.
Ver también
Categorías
Más información sobre Graph and Network Algorithms en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
