Small network Matlab code
Afficher commentaires plus anciens
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?
9 commentaires
KSSV
le 10 Oct 2016
You want us to type % before the lines?
Jan
le 10 Oct 2016
KSSV
le 10 Oct 2016
You have code in had....put breaking point and check yourself..google it.
Jan
le 10 Oct 2016
KSSV
le 10 Oct 2016
sort will arrange the given array in ascending or descending order as requested.
Jan
le 10 Oct 2016
Image Analyst
le 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
le 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.
Réponses (2)
Walter Roberson
le 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 commentaires
Walter Roberson
le 14 Oct 2016
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()
Walter Roberson
le 16 Oct 2016
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.
Walter Roberson
le 17 Oct 2016
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.
Jan
le 18 Oct 2016
Walter Roberson
le 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.
veeralakshmi s
le 17 Sep 2019
0 votes
s = repelem((1:N)',1,K);
Catégories
En savoir plus sur Descriptive Statistics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!