7
\$\begingroup\$

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

New contributor
Mattex is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Wheat Wizard Nov 30 at 13:55
  • 2
    \$\begingroup\$ 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]? \$\endgroup\$ – Robin Ryder Nov 30 at 13:59
  • \$\begingroup\$ That's right, many thanks! \$\endgroup\$ – Mattex Nov 30 at 14:07
  • 1
    \$\begingroup\$ Is this even a challenge for others or is it a legitimate question OP has? \$\endgroup\$ – Mark Jeronimus Dec 2 at 7:01
  • \$\begingroup\$ @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. \$\endgroup\$ – Wheat Wizard Dec 2 at 14:23
7
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I see the warning and understand why it's there but would this create any issues down the line? \$\endgroup\$ – Mattex Nov 30 at 14:24
  • 2
    \$\begingroup\$ @Mattex I can't think of any, apart from how tough it is to read and understand what's going on, lol. \$\endgroup\$ – Giuseppe Nov 30 at 14:54
  • \$\begingroup\$ @Mattex updated my answer. \$\endgroup\$ – Giuseppe Nov 30 at 15:34
  • 3
    \$\begingroup\$ 20 bytes with diag instead of matrix. \$\endgroup\$ – Robin Ryder Dec 1 at 17:30
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ – Giuseppe Dec 1 at 18:40
5
\$\begingroup\$

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!

\$\endgroup\$
  • 2
    \$\begingroup\$ In the first solution, you don't need the initial 0. \$\endgroup\$ – Robin Ryder Nov 30 at 16:37
4
\$\begingroup\$

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.

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

Your Answer

Mattex is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.