Find position of cell array within another cell array or table or in a structure
39 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Fangjun Jiang
約2時間 前
Providing an example of the data would be most helpful to understand the need. You can construct a simplifed data using code.
採用された回答
Steven Lord
約8時間 前
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
2 件のコメント
Steven Lord
約6時間 前
If you wanted to look for data in the custom attribute(s) and use that to select an edge or a set of edges, I'd use standard operations on one or more of the variables in the Edges table.
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)
whichEdgesHaveHighEnoughWeight = find(G.Edges.Weight > 25)
Now I could use this to highlight the appropriate edges in the plot, as a potential example:
h = plot(G, EdgeLabel = G.Edges.Weight);
highlight(h, ...
G.Edges.EndNodes(whichEdgesHaveHighEnoughWeight, 1), ...
G.Edges.EndNodes(whichEdgesHaveHighEnoughWeight, 2), EdgeColor = 'r')
その他の回答 (1 件)
Fangjun Jiang
2025 年 12 月 17 日 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')
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
