标签: r matrix
我有一个布尔条目矩阵,如下所示:
M= [,1] [,2] [,3] [1,] 1 1 0 [2,] 1 1 1 [3,] 0 1 1 [4,] 0 0 0 [5,] 1 0 0
我会用以下方式绘制这个矩阵。如果M [i,j]等于1,则代码在点(i,j)处标记点上的点。否则,如果M [i,j]等于0,那么代码什么都不做。
答案 0 :(得分:1)
我们的想法是获取行/列索引 - which arr.ind = TRUE将获得
which
arr.ind = TRUE
i1 <- which(M !=0, arr.ind = TRUE)
然后plot&#39; i1&#39;标记点
plot
plot(x=i1[,1], y = i1[,2], xlab = 'rowIndex', ylab = 'colIndex', pch = 20, col = 'red')
M <- structure(c(1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0), .Dim = c(5L, 3L))