Small network Matlab code

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

You want us to type % before the lines?
Yes, I'd like to see the 12 lines what they are supposed to do. This example is important for me to understand. Thank you.
Jan
Jan on 10 Oct 2016
Edited: Jan on 10 Oct 2016
I'd be very glad for that, otherwise I cannot move from the point.At least some hints would be great.
You have code in had....put breaking point and check yourself..google it.
So can you please at least give me a hint what do these lines?
[~, ind] = sort(newTargets, 'descend');
t(source, switchEdge) = ind(1:nnz(switchEdge));
sort will arrange the given array in ascending or descending order as requested.
And what is the purpose of ~ there? It stores negated sort somewhere?
It says to ignore the sorted values - throw them away. Only the indexes, called "ind" are returned and saved/captured.
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)

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

Jan
Jan on 14 Oct 2016
Edited: Jan on 14 Oct 2016
It remains only unclear to me now why switchEdge works correctly for the rank in "__descending__ order" of that position in newTargets? Rewiring should be done uniformly with probability beta to all targets, right? Thank you for the very detailed and friendly-to-read explanation!
beta is used to select which nodes are rewired to, but that leaves open the question of the order they should be connected.
The descending sort is so that you effectively exclude the 0s created by newTargets(t(source, ~switchEdge)) = 0;
I have not worked out exactly what the code is doing, but it looks to me as if it could probably be simplified by using randperm()
Jan
Jan on 16 Oct 2016
Edited: Jan on 16 Oct 2016
I'm slowly proceeding to finish to understand the code with your kind help;There are at this moment still 3 left uknown points :-(
[1] So what the program does here?
s = repelem((1:N)',1,K);
t = s + repmat(1:K,N,1);
t = mod(t-1,N)+1;
[2] What is a "mean node" and why it has degree 2*K? (my English is not good enough)
[3] and finally these two lines: esp. why do we need initializate newTargets(source) to 0?Is it to avoid self-loops?
newTargets(source) = 0;
newTargets(s(t==source)) = 0;
After
s = repelem((1:N)',1,K);
t = s + repmat(1:K,N,1);
then t is going to be an N x K matrix in which each element is the sum of its indices. Then t = mod(t-1,N)+1 "wraps" each entry at N, so 1, 2, 3, ... N, 1, 2, 3, ...
This is putting into effect
% Connect each node to its K next and previous neighbors. This constructs
% indices for a ring lattice.
'What is a "mean node" and why it has degree 2*K?'
You broke up the phrase at the wrong place. It is not "[mean node] of [degree 2*K]", it is "(mean [node degree]) of 2*K" . "mean" is "average". So the code constructs a graph in which, on average, each node will have degree 2*K
"and finally these two lines: esp. why do we need initializate newTargets(source) to 0?"
It is to allow you to ignore those locations when you do the descending sort. And yes, it does prevent self-loops.
Jan
Jan on 17 Oct 2016
Edited: Jan on 17 Oct 2016
How it follows from the code that the average "node degree" will be 2*K? Is the rewiring probability beta already taken into account here in the number 2*K?Average and mean may stand for either statistical/probabilistic mean (=average) OR physical position that it is surrounded by 2*K neighbourhoods . Which one it is the case here?
mean would be statistical in this case; they would have left out the word "mean" if they meant "always".
I do not know how it follows that it is the average node degree.
But statistical mean should have in its definition the random number beta. 2*K is a constant meaning that the mean is deterministic. The expected value should be something like 2*K+beta*K, if the rewiring is with probability beta from given K edges?
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.

Asked:

Jan
on 10 Oct 2016

Edited:

on 17 Sep 2019

Community Treasure Hunt

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

Start Hunting!