Related to vectors ?
1 次查看(过去 30 天)
显示 更早的评论
Hello all, I am working on a algorithm which is related to wireless communication wherein the source transmits signals to target.
These source and target are stored in two vectors as shown:
sr = [1,2,2,2,3,3,3,4,5]; % various possible source
ta = [2,3,6,8,6,4,7,6,6]; % various possible targets
So signal can be transmitted from 1st element of sr to 1st element of ta, 2nd element of sr to 2nd element of ta . Like wise from 9th element of sr to 9th element of ta.
My query is that how can we move between the elements of sr to elements of ta as I need to check whether the signal is transmitted or not.
2 个评论
John D'Errico
2023-5-5
You need to expain far more clearly the problem. What does "moving between the elements" mean here? Exactly what result would you expect to see from that example?
回答(1 个)
Steven Lord
2023-5-5
As a guess, do you want to know if you can reach all the targets from one of the sources? You could use a graph object (assuming communication is bidirectional) or digraph (if it's one-way.)
sr = [1,2,2,2,3,3,3,4,5]; % various possible source
ta = [2,3,6,8,6,4,7,6,6]; % various possible targets
g = graph(sr, ta);
plot(g)
To tell if you can get there from here take a look at the distances function.
d = distances(g)
There are no Inf values in d so each node is reachable from each other node. If we were to delete the edge between 5 and 6:
g2 = rmedge(g, 5, 6);
plot(g2)
d2 = distances(g2)
5 is no longer reachable and so you see Inf values in d2 indicating that fact. There are other functions on graph objects that may also be of use (like conncomp) -- take a look at the graph documentation page and the section that contains that page for more information.
c = conncomp(g2, OutputForm = 'cell')
3 个评论
Steven Lord
2023-5-10
This is basic indexing.
sr = [1,2,2,2,3,3,3,4,5]; % various possible source
ta = [2,3,6,8,6,4,7,6,6]; % various possible targets
for whichElement = 1:numel(sr)
fprintf("Source %d goes to target %d.\n", sr(whichElement), ta(whichElement))
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!