How to make a matrix in R that its inputs are equal to i * j?

Asked

Viewed 344 times

1

I want a Generic matrix 10x10, that their entries are equal to i*j, where i is the column and j the line

  • 5

    Hi Elipe, welcome to Stackoverflow in Portuguese! Please clarify your question better, show what you already know/have tried and what you still need to know. I don’t know R, but his doubt can mean a lot of things: "can’t he make a list?" " he doesn’t know how to create an array of size X?" "he can create but can’t put the desired values in it?" etc. Putting more context the question is easier to help you, and your question will probably be better received on the site (usually we expect a minimum understanding of the subject, although we are receptive to beginner level questions).

  • 3

    @mgibsonbr I think there is no need to close this question, although it is not perfect, there is not much mystery on how to assemble an array 10x10 with elements i*j as we can see in the answers.

  • 1

    @Carloscinelli I agree! I didn’t vote to close, and given the good answers I’m willing to reopen it if they close.

  • The question may not be extensive or demonstrate a previous research or effort, but it is very clear, because it is simple and objective. Maybe you deserve the votes you received, but I don’t agree with the closure.

2 answers

5

An alternative to applying a function in all combinations of 2 vectors is the function outer(). In the case:

 > outer(1:10, 1:10, '*')
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    2    3    4    5    6    7    8    9    10
 [2,]    2    4    6    8   10   12   14   16   18    20
 [3,]    3    6    9   12   15   18   21   24   27    30
 [4,]    4    8   12   16   20   24   28   32   36    40
 [5,]    5   10   15   20   25   30   35   40   45    50
 [6,]    6   12   18   24   30   36   42   48   54    60
 [7,]    7   14   21   28   35   42   49   56   63    70
 [8,]    8   16   24   32   40   48   56   64   72    80
 [9,]    9   18   27   36   45   54   63   72   81    90
[10,]   10   20   30   40   50   60   70   80   90   100
  • 1

    Cool this function, I’ve never seen it. I like it this way here tbm: 1:10 %o% 1:10

3

You can do it like this:

> x <- matrix(rep(1:10, each = 10), ncol = 10, byrow = T)
> apply(x, 1, function(x) x*1:10)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    2    3    4    5    6    7    8    9    10
 [2,]    2    4    6    8   10   12   14   16   18    20
 [3,]    3    6    9   12   15   18   21   24   27    30
 [4,]    4    8   12   16   20   24   28   32   36    40
 [5,]    5   10   15   20   25   30   35   40   45    50
 [6,]    6   12   18   24   30   36   42   48   54    60
 [7,]    7   14   21   28   35   42   49   56   63    70
 [8,]    8   16   24   32   40   48   56   64   72    80
 [9,]    9   18   27   36   45   54   63   72   81    90
[10,]   10   20   30   40   50   60   70   80   90   100

First I create a matrix x with all lines equal to the vector 1, 2, 3,.., 10. Then using the function apply, multiply each line by vector 1:10. Note that in R, when you multiply vectors, it does element-by-element multiplication, so this account works.

Another way to do it would be to create the matrix x with all the columns being 1,2,3, .., 10. And then multiply it by vector 1:10. This will also work, but is less intuitive as it depends on an important R concept, called recycling.

x <- matrix(rep(1:10, each = 10), ncol = 10)
x*1:10

As @mgibsonbr commented, in the next few questions try to explain a little more what you tried, and exactly what you are not getting.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.