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

8

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}\$

Mattex

Posted 2019-11-30T13:49:08.083

Reputation: 89

3

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 [tag: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.

– Post Rock Garf Hunter – 2019-11-30T13:55:37.533

2Am 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 – 2019-11-30T13:59:22.703

That's right, many thanks! – Mattex – 2019-11-30T14:07:30.813

1Is this even a challenge for others or is it a legitimate question OP has? – Mark Jeronimus – 2019-12-02T07:01:56.940

@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. – Post Rock Garf Hunter – 2019-12-02T14:23:40.463

Answers

7

R, 24 23 22 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.

Giuseppe

Posted 2019-11-30T13:49:08.083

Reputation: 21 077

I see the warning and understand why it's there but would this create any issues down the line? – Mattex – 2019-11-30T14:24:43.310

2@Mattex I can't think of any, apart from how tough it is to read and understand what's going on, lol. – Giuseppe – 2019-11-30T14:54:07.630

@Mattex updated my answer. – Giuseppe – 2019-11-30T15:34:29.440

many thanks buddy. – Mattex – 2019-12-01T08:46:56.370

320 bytes with diag instead of matrix. – Robin Ryder – 2019-12-01T17:30:27.200

1@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 – 2019-12-01T18:40:06.877

5

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!

Nick Kennedy

Posted 2019-11-30T13:49:08.083

Reputation: 11 829

2In the first solution, you don't need the initial 0. – Robin Ryder – 2019-11-30T16:37:48.920

4

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.

Robin Ryder

Posted 2019-11-30T13:49:08.083

Reputation: 6 625

My mistake, matrix now corrected in quoestion and Robin Ryder's answer was of help. – Mattex – 2019-11-30T14:12:27.610