Related to vectors ?
조회 수: 2 (최근 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!