-1

I came across the Q&A: Possible-clique-numbers-of-a-regular-graph.

I have a vec vector and I need to create a adjacency matrix A such that A[i,j]=1 if i−j mod n is an element of vec and A[i,j]=0 otherwise.

My attempt:

k   <- 4
n   <- 10
vec <- c(seq(-k+1, -1), seq(1, k+1))

A <- matrix(0, n, n)

for (i in 1:n)
for (j in 1:n)
A[i,j] <- if((i - j) %% n in vec) 1

I get this error:

Error: unexpected 'in' in:
"for (j in 1:n)
A[i,j] <- if((i - j) %% n in"

Question: how to create an (n x n)-adjacency matrix based on the condition?

Expected result is:

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nick
  • 1,086
  • 7
  • 21

2 Answers2

1

Is this what you're after?

k   <- 4
n   <- 10
vec <- c(seq(-k+1, -1), seq(1, k+1))

outer(vec, vec, FUN = function(i, j) ifelse((i - j %% n) %in% vec, 1, 0))
#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#[1,]    0    0    0    0    0    0    0    0
#[2,]    0    0    0    1    0    0    0    0
#[3,]    0    0    0    1    1    0    0    0
#[4,]    0    0    0    0    1    1    1    0
#[5,]    0    0    0    1    0    1    1    1
#[6,]    0    0    0    1    1    0    1    1
#[7,]    1    0    0    1    1    1    0    1
#[8,]    1    1    0    1    1    1    1    0
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • I have added the expected output. – Nick Sep 09 '19 at 03:40
  • @Nick Why is your expected output matrix symmetric? `i - j %% n` is not symmetric in `i` and `j`. So I expected the output matrix to not be symmetric. Am I missing something? Also, there was a typo in my code (I missed a bracket) which I've fixed. – Maurits Evers Sep 09 '19 at 03:54
  • I am thinking about symmetric/non_symmetric. Why is the size 8 by 8, but is not 10 by 10? – Nick Sep 09 '19 at 04:01
  • @Nick It's `8x8` because `vec` has only 8 elements. Why do you expect to get a `10x10` matrix? – Maurits Evers Sep 09 '19 at 05:02
1

Try this.

k   <- 4
n   <- 10
vec <- c(seq(1, k-1), seq(n-k+1, n-1))  # changed

A <- matrix(0, n, n)
for (i in 1:n) for (j in 1:n)  A[i,j] <- (abs(i-j%%n)) %in% vec # changed
bluk
  • 400
  • 6
  • 15
  • I'm not sure I follow your logic here. From what I understood OP needs to create an adjacency matrix (with some distance metric) based on `vec`. So the resulting matrix will have dimension `m x m` where `m` is the length of `vec`. In your case, you populate an `n x n` matrix where `n` is 10 and `length(vec)` is 6. There seems to be no connection between `n` and `length(vec)`. You could've equally populated a `10000 x 10000` matrix (just set `n <- 10000` and re-run your `for` loop). – Maurits Evers Sep 09 '19 at 08:50
  • @MauritsEvers Guess the way this question is stated has led to some different interpretations. My understanding is that OP is trying to get an adjacency matrix which replicates the graph in the accepted answer of his quoted question, so when `n=10`, it will give a matrix corresponding to how the vertexes of a 10-side regular polygon connect to each other, while `vec` here is merely auxiliary. – bluk Sep 09 '19 at 09:16
  • Yeah I follow your reasoning. Sadly an ambiguous problem statement from OP. – Maurits Evers Sep 09 '19 at 09:17