Small network Matlab code

6 次查看(过去 30 天)
Jan
Jan 2016-10-10
Can someone please comment each line (starting with "s = repelem((1:N)',1,K);") of the following Matlab code computing the small n,k network with prob. beta shortcuts?
[SL: snipped the WattsStrogatz function copied from this example on the MathWorks website.]
  9 个评论
Image Analyst
Image Analyst 2016-10-14
It says to ignore the sorted values - throw them away. Only the indexes, called "ind" are returned and saved/captured.
Marc Jakobi
Marc Jakobi 2016-10-14
If you are not sure what a function does, you can type:
doc functionname
in the command window and you will find a description, usually with a bunch of examples.

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2016-10-14
switchEdge = rand(K, 1) < beta;
rand(K,1) is a K by 1 vector of uniformly distributed random numbers in the range 0 to 1 (exclusive). Testing the vector against beta returns true (numeric equivalent 1) up to the probability beta, and false (numeric equivalent 0) up after beta. So the line creates a logical vector that is true with probability beta.
[~, ind] = sort(newTargets, 'descend');
sorts newTargets in descending order, and throws away the sorted values (the ~ says throw-away) and returns only the ordering -- so ind(1) was the location that had the highest value, ind(2) the next highest, and so on up to ind(end) was the place with lowest value.
t(source, switchEdge) = ind(1:nnz(switchEdge));
nnz(switchEdge) is the number of non-zero locations in switchEdge, which corresponds to the number of locations that were true in switchEdge . Because logical vectors are all 0 and 1, another way of calculating nnz(switchEdge) would be sum(switchEdge).
1:nnz(switchEdge) is then the vector 1, 2, 3, 4, ... ending at the number of true locations in switchEdge.
ind(1:nnz(switchEdge)) uses that vector as indices into ind -- so in other words, the code is taking the first nnz(switchEdge) entries out of ind
switchedge is a logical vector, not a numeric vector (it just has numeric equivalents), and when it appears as an index as in t(source, switchEdge) the elements of switchEdge that are true are selected as the source or destination for the operation. The number selected would be the number of locations that are true, which happens to correspond to nnz(switchEdge), which is the same number of values that are on the right hand side, so the number of source elements and the number of destination elements balances and the operation is valid. In an assignment, locations that are not selected are skipped, left unchanged.
The effect of t(source, switchEdge) = ind(1:nnz(switchEdge)); is to construct a row in t in which the locations in the row that correspond to switchEdge being true are set to equal the rank (in descending order) of that position in newTargets, and with the unselected locations being left untouched.
  9 个评论
Walter Roberson
Walter Roberson 2016-10-18
The code links to adjacent neighbors first and then randomly rewires connections. If the probability of losing a connection is the same as the probability of gaining a connection then the average degree will be the same.
Jan
Jan 2017-8-31
编辑:Jan 2017-8-31
From time to time the code creates a disconnected graph. What is the probability of this event depending on (N,K,beta) and what conditions must occur simultaneously during the code to this happen?

请先登录,再进行评论。


veeralakshmi s
veeralakshmi s 2019-9-17
s = repelem((1:N)',1,K);

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by