Clear Filters
Clear Filters

Small network Matlab code

6 views (last 30 days)
Jan
Jan on 10 Oct 2016
Edited: Steven Lord on 17 Sep 2019
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 Comments
Image Analyst
Image Analyst on 14 Oct 2016
It says to ignore the sorted values - throw them away. Only the indexes, called "ind" are returned and saved/captured.
Marc Jakobi
Marc Jakobi on 14 Oct 2016
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.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 14 Oct 2016
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 Comments
Walter Roberson
Walter Roberson on 18 Oct 2016
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 on 31 Aug 2017
Edited: Jan on 31 Aug 2017
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?

Sign in to comment.


veeralakshmi s
veeralakshmi s on 17 Sep 2019
s = repelem((1:N)',1,K);

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!