Cell array with adjacents elements by a “from” vector and a “to” vector
2 次查看(过去 30 天)
显示 更早的评论
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(
))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697366/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697366/image.png)
3 -> 7 (
)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697376/image.png)
2 -> 8 (
)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697381/image.png)
4 -> 9 (
)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697386/image.png)
4 -> 10 (
)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697391/image.png)
1 -> 11 (
)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697396/image.png)
5 -> 12 (
)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697401/image.png)
On the other hand:
source (find(
)) -> target (
)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697406/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697406/image.png)
7 (
) -> 1
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697416/image.png)
8 (
) -> 6
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697421/image.png)
9 (
) -> 2
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697426/image.png)
10 (
) -> 8
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697431/image.png)
11 (
) -> 9
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697436/image.png)
12 (
) -> 1
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1697441/image.png)
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
0 个评论
回答(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 个评论
Steven Lord
2024-5-17
A=[NaN NaN NaN NaN NaN NaN 3 2 4 4 1 5];
source = A(~isnan(A))
target = find(~isnan(A))
D = graph(source, target)
plot(D)
dist = distances(D, 4)
reachable = find(isfinite(dist))
So you can get from 4 to 4, 9, or 10.
allpaths(D, 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)
plot(D)
Now there are more ways to get from 4 to 10.
allpaths(D, 4, 10)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!