## Adjacency list by a “from” vector and a “to” vector

I have 2 vectors, A and B of equal dimension. Each value of A indicates the origin element of the element given by its position. Each value of B indicates the destination 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 destination elements, and its position is the origin element. In this case it would be like this:

Origin () -> Destination (find())

3 -> 7 ()

2 -> 8 ()

4 -> 9 ()

4 -> 10 ()

1 -> 11 ()

5 -> 12 ()

On the other hand:

Origin (find()) -> Destination ()

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 advanceI have 2 vectors, A and B of equal dimension. Each value of A indicates the origin element of the element given by its position. Each value of B indicates the destination 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 destination elements, and its position is the origin element. In this case it would be like this:

Origin () -> Destination (find())

3 -> 7 ()

2 -> 8 ()

4 -> 9 ()

4 -> 10 ()

1 -> 11 ()

5 -> 12 ()

On the other hand:

Origin (find()) -> Destination ()

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 I have 2 vectors, A and B of equal dimension. Each value of A indicates the origin element of the element given by its position. Each value of B indicates the destination 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 destination elements, and its position is the origin element. In this case it would be like this:

Origin () -> Destination (find())

3 -> 7 ()

2 -> 8 ()

4 -> 9 ()

4 -> 10 ()

1 -> 11 ()

5 -> 12 ()

On the other hand:

Origin (find()) -> Destination ()

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 adjacency list MATLAB Answers — New Questions