# What is a short code for generating this matrix in R?

I can see that it is 101 'moving to the right' but I cannot think of a short way to generate it in R language.

$$\\begin{bmatrix} 0 & 1 & 0 & 0 & 0 & 0 \\ 1 & 0 & 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 & 1 & 0 \\ \end{bmatrix}\$$

• Welcome to the site. Your title and your tags mention R, but the body of your question does not. This is a question in R right? If so this is a tips question (you can learn about them here), It would be best if you included your own best try so that answerers can know what level you are currently at. – Ad Hoc Garf Hunter Nov 30 '19 at 13:55
• Am I right in assuming that there is a typo on the last row of your figure? Shouldn't the last 1 be at position [6,5] instead of [6,6]? – Robin Ryder Nov 30 '19 at 13:59
• That's right, many thanks! – Mattex Nov 30 '19 at 14:07
• Is this even a challenge for others or is it a legitimate question OP has? – Mark Jeronimus Dec 2 '19 at 7:01
• @MarkJeronimus I would assume that the challenge writer knows how to write up a matrix in long form. I prefer to assume good faith until evidence appears otherwise, but even if this question was asked in bad faith I'm not sure this matters, it looks like this is an interesting enough question to warrant several answers so even if the OP is not interested in golfing techniques it can still teach others. – Ad Hoc Garf Hunter Dec 2 '19 at 14:23

# R, 242322 20 bytes

diag(0,6)+c(!-1:4,1)


Try it online!

Just use R's recycling! This does give a warning, but otherwise is fine.

Thanks to Robin Ryder for the 2 byte golf.

• I see the warning and understand why it's there but would this create any issues down the line? – Mattex Nov 30 '19 at 14:24
• @Mattex I can't think of any, apart from how tough it is to read and understand what's going on, lol. – Giuseppe Nov 30 '19 at 14:54
• @Mattex updated my answer. – Giuseppe Nov 30 '19 at 15:34
• 20 bytes with diag instead of matrix. – Robin Ryder Dec 1 '19 at 17:30
• @RobinRyder nice! That is a good tip for generating a square matrix of zeros! I'm on mobile right now, so feel free to edit that in if you like – Giuseppe Dec 1 '19 at 18:40

# R, 30 27 bytes

+!abs(outer(1:6,1:6,-))-1


Try it online!

Another alternative. Note this matrix shows which integers between 1 and 6 are exactly 1 apart. (i.e. $$\|i - j| = 1\$$).

Thanks to @RobinRyder for saving a byte! I was forgetting + could be used as a unary operator. Thanks also to @Giuseppe for saving 2 bytes!

If a matrix of logical values is permissible, then this also works:

# R, 26 bytes

abs(outer(1:6,1:6,-))==1


Try it online!

This is also the closest to my shortest Jelly solution to this:

# Jelly, 5 bytes

6ạþ=1


Try it online!

• In the first solution, you don't need the initial 0. – Robin Ryder Nov 30 '19 at 16:37

# R, 34 bytes

x=cbind(rbind(0,diag(5)),0)
x+t(x)


Try it online!

Outputs

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    0    1    0    0    0    0
[2,]    1    0    1    0    0    0
[3,]    0    1    0    1    0    0
[4,]    0    0    1    0    1    0
[5,]    0    0    0    1    0    1
[6,]    0    0    0    0    1    0


Builds a $$\5\times5\$$ diagonal matrix, pads with 0s at the top and the right, and adds it to its own transpose.

• My mistake, matrix now corrected in quoestion and Robin Ryder's answer was of help. – Mattex Nov 30 '19 at 14:12