Cell array with adjacents elements by a “from” vector and a “to” vector

2 visualizaciones (últimos 30 días)
I have 2 vectors, A and B of equal dimension. Each value of A indicates the source element of the element given by its position. Each value of B indicates the target element of the element given by its position. Some values of A and B are NaN, but they have the same indices (they occupy the same positions in A and B). Some values of A and B may be repeated.
For example:
A=[NaN NaN NaN NaN NaN NaN 3 2 4 4 1 5 ... ];
B=[NaN NaN NaN NaN NaN NaN 1 6 2 8 9 1 ... ];
I would like to get a cell array, C, that is an adjacency list of these elements. That is, a cell array where each cell is a vector with the target elements, and its position is the source element. In this case it would be like this:
source () -> target (find())
3 -> 7 ()
2 -> 8 ()
4 -> 9 ()
4 -> 10 ()
1 -> 11 ()
5 -> 12 ()
On the other hand:
source (find()) -> target ()
7 () -> 1
8 () -> 6
9 () -> 2
10 () -> 8
11 () -> 9
12 () -> 1
Therefore:
C={[11], [8], [7], [9, 10], [12], [1], [6], [2], [8], [9], [1], ...};
I have managed to do it with “for”, but A and B are very large and it takes too long to calculate. Could it be done in batch?
Thanks in advance

Respuestas (1)

Steven Lord
Steven Lord el 17 de Mayo de 2024
What are you hoping to do with this list? Depending on the specific operations you're looking to perform you may want to create a graph or digraph from your list of sources and targets and then use the functions available for Graph and Network Algorithms to manipulate the graph or digraph.
  5 comentarios
Steven Lord
Steven Lord el 17 de Mayo de 2024
A=[NaN NaN NaN NaN NaN NaN 3 2 4 4 1 5];
source = A(~isnan(A))
source = 1x6
3 2 4 4 1 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
target = find(~isnan(A))
target = 1x6
7 8 9 10 11 12
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
D = graph(source, target)
D =
graph with properties: Edges: [6x1 table] Nodes: [12x0 table]
plot(D)
dist = distances(D, 4)
dist = 1x12
Inf Inf Inf 0 Inf Inf Inf Inf 1 1 Inf Inf
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
reachable = find(isfinite(dist))
reachable = 1x3
4 9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
So you can get from 4 to 4, 9, or 10.
allpaths(D, 4, 10)
ans = 1x1 cell array
{[4 10]}
In this simple case the list of all paths is short. Let's add a few more edges.
D = addedge(D, [4, 10], 8)
D =
graph with properties: Edges: [8x1 table] Nodes: [12x0 table]
plot(D)
Now there are more ways to get from 4 to 10.
allpaths(D, 4, 10)
ans = 2x1 cell array
{[4 8 10]} {[ 4 10]}
Sergio Rojas Blanco
Sergio Rojas Blanco el 18 de Mayo de 2024
Stephen, thank you very much for your answer, but I must consider the vector B to obtain all the adjacency relations.
I use VISSIM to get network information. A road (link) can be a normal link or a connector. A connector joins 2 adjacent links. VISSIM provides information about the source link (A), the destination link (B) and whether it is a connector.
It seems like it could be done with findgroups and splitapply, but I can't figure it out.

Iniciar sesión para comentar.

Categorías

Más información sobre Graph and Network Algorithms en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by