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

조회 수: 4 (최근 30일)
Sergio Rojas Blanco 2024년 5월 17일
편집: Sergio Rojas Blanco 2024년 5월 20일
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?

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Steven Lord 2024년 5월 17일
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이전 댓글 3개 표시이전 댓글 3개 숨기기
Steven Lord 2024년 5월 17일
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 2024년 5월 18일
Stephen, thank you very much for your answer, but I must consider the vector B to obtain all the adjacency relations.
It seems like it could be done with findgroups and splitapply, but I can't figure it out.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

R2024a

### Community Treasure Hunt

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

Start Hunting!

Translated by