Loop with R arrays

Asked

Viewed 45 times

3

I need to create a matrix that has all the coordinates for a 128x128 matrix in R

if it were a 3x3 matrix, ex:

    1    2    3
1   a    b    c

2   d    e    f

3   g    h    i

I would need the following matrix

    x   y

1   1   1  

2   1   2

3   1   3 

4   2   1

5   2   2 

6   2   3

7   3   1

8   3   2

9   3   3

how can I make a repeat code to reproduce this?

  • How are your data? You only need the coordinates of the matrix (i.e., row and column numbers) or the names of the columns and rows are coordinates (e.g., geographical coordinates)?

1 answer

4

It is not necessary to create a loop for this. The function expand.grid does exactly what is requested:

expand.grid(1:3, 1:3)
  Var1 Var2
1    1    1
2    2    1
3    3    1
4    1    2
5    2    2
6    3    2
7    1    3
8    2    3
9    3    3

Just replace 3 by 128 in the above code and everything will be solved.

Browser other questions tagged

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