Graph,Smallnetwrok,WattsStrogatz, confused command
조회 수: 11 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
Geoff Hayes
2018년 3월 9일
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
Geoff Hayes
2018년 3월 9일
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?
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!