Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

plot coordinates from two vectors into a 0,1 matrix

$
0
0

I have two vectors of equal length

a <- 1:10
b <- sample.int(10,size=10)

I would like to plot them into a matrix of the same length (10) where a is the row coordinate, b the column coordinate, with the value 1 for the coordinates and 0 for everything else. I have below a way to do this using a for loop, but was hoping to do this without a loop. Thanks!

matrix01 <- matrix(0, nrow = 10, ncol = 10)

for(i in 1:10) {
   matrix01[i, b[i]] = 1
}

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>