1

I would like to create a 5x5 matrix in R using two for loops. I have 5 numbers p_j and q_i with i and j in {1,2,3,4,5}. I would like to create a matrix with where the element in (j,i) is given by p_j * q_j. j-th row and i-th column. So, first of all I would like to create an empty matrix m and then filling the matrix using the for loops.

for (i in 1:5){ for (j in 1:5){ } }

3
  • 5
    Must you use the double loop? Either p %*% t(q) or outer(p,q, `*`) seem like better solutions. Commented Jun 5, 2017 at 19:14
  • I am afraid, my R skills are not sufficient in order to understand your comment. Could you work out your comment as a second answer? Commented Jun 5, 2017 at 19:19
  • 1
    As an aside-- R indexes by [row, column], so you would want to assemble your matrix that way. Commented Jun 5, 2017 at 19:30

2 Answers 2

4

I will walk through two solutions that I proposed. Both of these solutions avoid any explicit looping. Mostly in R, if it is easy to avoid a loop, you probably should. First, let's get some example data.

set.seed(2017)
p = sample(5)
q = sample(5)
p
[1] 5 3 2 1 4
q
[1] 4 1 2 5 3

Here p and q are randomly generated. The set.seed part is so that we both get the same "random" numbers.

Solution 1 - matrix multiplication

p %*% t(q)
     [,1] [,2] [,3] [,4] [,5]
[1,]   20    5   10   25   15
[2,]   12    3    6   15    9
[3,]    8    2    4   10    6
[4,]    4    1    2    5    3
[5,]   16    4    8   20   12

%*% is the way to specify matrix multiplication in R.
p %*% t(q) multiplies the 5x1 matrix p by the 1x5 matrix t(q), the transpose of q, resulting in the 5x5 matrix with the desired answer.

Solution 2 - outer

outer(p,q, `*`)
     [,1] [,2] [,3] [,4] [,5]
[1,]   20    5   10   25   15
[2,]   12    3    6   15    9
[3,]    8    2    4   10    6
[4,]    4    1    2    5    3
[5,]   16    4    8   20   12

The function outer in r, creates the "outer product" of two vectors - that is, it takes all combinations of an element of p and an element of q and combines them using the function that you supplied, in this case *, exactly the calculation that you are asking for. Actually, this could have been written more succinctly as outer(p,q) because the default function to use to combine p & q is *.

Sign up to request clarification or add additional context in comments.

3 Comments

P and q are not elements in the global environment: there are 10 variables, named p_1, p_2, ... p_5, and q_1 ... q_5. Also, the question explicitly asks for a solution using two for loops. Even though it's not best practice in R, answering with a non-for-looping answer doesn't answer the question.
@ConCave I originally wrote this as a comment that noted it did not use loops. The OP asked me to include it as an answer.
Nevermind them, sorry about that. Thanks for providing a more detailed explanation of your comment.
2

Assuming you have variables named "p_1", "p_2" in the working environment:

mymatrix <- matrix(nrow = 5, ncol = 5)
for (i in 1:5) { 
  for (j in 1:5) { 
    mymatrix[i, j] <- get(paste("p_", j, sep="")) * 
                      get(paste("q_", i, sep=""))
  }
}

2 Comments

mymatrix[i, j] is the element in the i-th row and j-th column, right?
mymatrix[i, j] is the ith row and the jth column. I'm not sure exactly what arrangement of your numbers you want, or how your data is represented. If you have variables named "p_1", "p_2", etc, this answer will work, but that's absolutely not the best way to store your data in R. They should be 2 seperate vectors named p and q, and then you can access your numbers with p[1], p[2], etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.