What is the purpose of using the mod operator in this line of code?

Click For Summary
The mod operator in the code is used to create a cyclic pattern when populating the transition matrix for a random walk on an n-cycle. Specifically, it ensures that the indices wrap around when they exceed the size of the vector, allowing for a continuous loop. The expression "1 + (i:(n+i-1)) %% n" generates a sequence of indices that correctly references the elements of 'r1' while maintaining the circular structure. The print statements confirm the values of 'r1', the calculated indices, and the current row of 'tmat' being filled. This functionality is crucial for accurately modeling transitions in a cyclic graph.
user366312
Gold Member
Messages
88
Reaction score
3
TL;DR
Kindly explain the highlighted lines.
[CODE title="R code" highlight="7,11"]#==================================================================
# Build the transition matrix for random walk on n-cycle
#------------------------------------------------------------------
n <- 25 # we are creating (nXn) transition matrix
r1 <- c(0, 1/2, rep(0,n-3), 1/2) # create same values 22 times...
tmat <- matrix(0, nrow=n, ncol=n) # create a 0-matrix
tmat[1,] <- r1 # inject values in 'r1' into 'tmat' along the 1st row

for (i in 1:(n-1))
{
tmat[26-i,] <- r1[1 + (i:(n+i-1)) %% n] # ? mod operator
}
#=================================================================[/CODE]

What are being done in line#11?

How are elements being selected using mod operator?
 
Technology news on Phys.org
It really helps to print results and intermediate calculations.
I ran it with this code right after the calculation of r1:
[CODE highlight="R code to print r1 values"]
print("r1")
print( r1 )
[/CODE]
And this at the bottom of the loop:
[CODE title="R print code at bottom of loop"]
print("1 + (i:(n+i-1)) %% n")
print(1 + (i:(n+i-1)) %% n)
print("26-i")
print(26-i)
print("tmat")
print(tmat[26-i,])
[/CODE]

The beginning of the print results are:
[CODE title="Start of print results"]
[1] "r1"
[1] 0.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5
[1] "1 + (i:(n+i-1)) %% n"
[1] 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 1
[1] "26-i"
[1] 25
[1] "tmat"
[1] 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0
[1] "1 + (i:(n+i-1)) %% n"
[1] 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 1 2
[1] "26-i"
[1] 24
[1] "tmat"
[1] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.5
[/CODE]
 
  • Like
Likes sysprog and jedishrfu
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K