1

Create the following 2 matrices utilizing the special form of the matrix - meaning that your solution should be generalized to creating a larger table with the same structure, without requiring to type all the entries within the table. Hint: You will find the row() and col() functions useful.

enter image description here

All I've done for now is this:

>i=0:5
>matrix(i,nrow=i,ncol=i)

but it is not complete. You have to somehow utilize the functions row() and col().

Phil
  • 7,287
  • 3
  • 36
  • 66
Stathis
  • 11
  • 3
  • 1
    Welcome to StackOverflow. [Please see here on how to make a good reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Can you provide your matrices using `dput()`? – jrcalabrese Dec 10 '22 at 20:50

1 Answers1

1

For the first matrix

> n <- 6

> (embed(seq(2 * n - 1) - 1, n)[, n:1]) %% n
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    0    1    2    3    4    5
[2,]    1    2    3    4    5    0
[3,]    2    3    4    5    0    1
[4,]    3    4    5    0    1    2
[5,]    4    5    0    1    2    3
[6,]    5    0    1    2    3    4

For the second matrix

> n <- 9

> embed(seq(2 * n - 1), n) %% n
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    0    8    7    6    5    4    3    2    1
 [2,]    1    0    8    7    6    5    4    3    2
 [3,]    2    1    0    8    7    6    5    4    3
 [4,]    3    2    1    0    8    7    6    5    4
 [5,]    4    3    2    1    0    8    7    6    5
 [6,]    5    4    3    2    1    0    8    7    6
 [7,]    6    5    4    3    2    1    0    8    7
 [8,]    7    6    5    4    3    2    1    0    8
 [9,]    8    7    6    5    4    3    2    1    0
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81