So I have a 50*50 matrix full of 0's and I'm trying to add two 1's in randomized locations in the matrix. How would I go about this?
Asked
Active
Viewed 72 times
1 Answers
0
We may use sample
on the sequence of values and assign 1 by specifying size = 2
m1[sample(seq_along(m1), 2, replace = FALSE)] <- 1
-check the location
which(m1 == 1, arr.ind = TRUE)
row col
[1,] 40 47
[2,] 19 50
data
m1 <- matrix(0, 50, 50)

akrun
- 874,273
- 37
- 540
- 662