See basically you are using the fractions function that has derived from the MASS packages which can work with matrices. But you don't really need to have a double loop. Let us say that you have a matrix as such:
X <- diag(5)/5
X
[,1] [,2] [,3] [,4] [,5]
[1,] 0.2 0.0 0.0 0.0 0.0
[2,] 0.0 0.2 0.0 0.0 0.0
[3,] 0.0 0.0 0.2 0.0 0.0
[4,] 0.0 0.0 0.0 0.2 0.0
[5,] 0.0 0.0 0.0 0.0 0.2
You can also subset the rows and columns easily by the following code:
fractions(X)[1:3, 1:3]
[,1] [,2] [,3]
[1,] 1/5 0 0
[2,] 0 1/5 0
[3,] 0 0 1/5
If you are a beginner ad want to know more about R the do check out the following R programming tutorial.