Related to vectors ?

2 次查看(过去 30 天)
charu shree
charu shree 2023-5-5
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
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?
charu shree
charu shree 2023-5-10
Basically I just want to access the elements of 'sr' and 'ta'. For example can we use for loop to access 5th element of 'sr' (which is 3) and 5th element of 'ta' (which is 6).

请先登录,再进行评论。

回答(1 个)

Steven Lord
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)
d = 8×8
0 1 2 3 3 2 3 2 1 0 1 2 2 1 2 1 2 1 0 1 2 1 1 2 3 2 1 0 2 1 2 3 3 2 2 2 0 1 3 3 2 1 1 1 1 0 2 2 3 2 1 2 3 2 0 3 2 1 2 3 3 2 3 0
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)
d2 = 8×8
0 1 2 3 Inf 2 3 2 1 0 1 2 Inf 1 2 1 2 1 0 1 Inf 1 1 2 3 2 1 0 Inf 1 2 3 Inf Inf Inf Inf 0 Inf Inf Inf 2 1 1 1 Inf 0 2 2 3 2 1 2 Inf 2 0 3 2 1 2 3 Inf 2 3 0
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')
c = 1×2 cell array
{[1 2 3 4 6 7 8]} {[5]}
  3 个评论
Steven Lord
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
Source 1 goes to target 2. Source 2 goes to target 3. Source 2 goes to target 6. Source 2 goes to target 8. Source 3 goes to target 6. Source 3 goes to target 4. Source 3 goes to target 7. Source 4 goes to target 6. Source 5 goes to target 6.
charu shree
charu shree 2023-5-11
Thank u so much sir for ur response...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by