Effacer les filtres
Effacer les filtres

Graph,Smal​lnetwrok,W​attsStroga​tz, confused command

3 vues (au cours des 30 derniers jours)
Jan
Jan le 9 Mar 2018
Commenté : Jan le 9 Mar 2018
What is an intuitive difference between/purpose of these two lines in the following program?
newTargets(source) = 0;
newTargets(s(t==source)) = 0;
PROGRAM
function h = WattsStrogatz(N,K,beta)
% H = WattsStrogatz(N,K,beta) returns a Watts-Strogatz model graph with N
% nodes, N*K edges, mean node degree 2*K, and rewiring probability beta.
%
% beta = 0 is a ring lattice, and beta = 1 is a random graph.
% Connect each node to its K next and previous neighbors. This constructs
% indices for a ring lattice.
s = repelem((1:N)',1,K);
t = s + repmat(1:K,N,1);
t = mod(t-1,N)+1;
% Rewire the target node of each edge with probability beta
for source=1:N
switchEdge = rand(K, 1) < beta;
newTargets = rand(N, 1);
newTargets(source) = 0;
newTargets(s(t==source)) = 0;
newTargets(t(source, ~switchEdge)) = 0;
%
[~, ind] = sort(newTargets, 'descend');
t(source, switchEdge) = ind(1:nnz(switchEdge));
end
h = graph(s,t);
end
% Copyright 2015 The MathWorks, Inc.

Réponses (1)

Geoff Hayes
Geoff Hayes le 9 Mar 2018
Jan - source is a scalar from 1 to N and is used in
newTargets(source)
as an index. So
newTargets(source) = 0;
will set the element in newTargets at index source to zero. So a single element of the array is set to zero.
For the other, presumably, s and t are of the same dimension of newTargets. The line
t==source
will compare each element of t to the value of the scalar source and return a logical array of zeros and ones where a one indicates a match and a zero indicates no match. This logical array can then be used to retrieve all those elements of s where we have a one in the result t==source (see Using Logicals in Array Indexing for more details).
s(t==source)
The set of values of s that satisfy the above are then used as indices into newTargets to set all those elements of newTargets at indices s(t==source) to zero.
So
newTargets(source) = 0; % sets element of newTargets at index source to zero
newTargets(s(t==source)) = 0; % sets multiple elements of newTargets at indices s(t==source) to zero
  3 commentaires
Geoff Hayes
Geoff Hayes le 9 Mar 2018
Jan - I'm not familiar with the algorithm...I assumed that you wanted an explanation of what the difference was between the two lines of code. Have you looked at other implementations or a reference for the algorithm?
Jan
Jan le 9 Mar 2018
It is described in detail HERE.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Networks dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by