필터 지우기
필터 지우기

Small network Matlab code

조회 수: 6 (최근 30일)
Jan
Jan 2016년 10월 10일
편집: Steven Lord 2019년 9월 17일
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