以正确顺序填充矩阵的一种方法是使用矩阵转置。
创建一些数据:下三角相关矩阵输出
m <- cor(mtcars[1:4])
m[upper.tri(m)] <- NA
write.table(m, temp<-tempfile(), row.names = FALSE, col.names=FALSE, na="")
使用标准工具阅读
# Change `temp` to the path to your matrix file
inmat <- as.matrix(read.table(temp, fill=TRUE))
# Fill in upper triangle
inmatt <- t(inmat)
inmatt[lower.tri(inmatt, diag=FALSE)] <- inmat[lower.tri(inmat, diag=FALSE)]
检查
all.equal(cor(mtcars[1:4]), inmatt, check.attributes=FALSE)
好的,因为您正在从图像中读取,为了节省手动输入,您可以从图像中读取文本。
library(tesseract)
# Read image
engine <- tesseract(options = list(tessedit_char_whitelist = ".-0123456789"))
text <- ocr("https://i.stack.imgur.com/OHf3z.jpg", engine)
cat(text) # looks okay
cat(text, file=temp<-tempfile())
# Now try to fill matrix
v = as.numeric(unlist(strsplit(readLines(temp), " ")))
inmat <- diag(15) # just hard-code matrix dimension
inmat[upper.tri(inmat, diag=TRUE)] <- v
inmatt <- t(inmat)
inmatt[upper.tri(inmatt, diag=TRUE)] <- v
生产
inmatt
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,] 1.00 0.77 0.53 0.54 0.54 0.30 0.16 0.36 -0.11 -0.10 -0.02 0.14 0.09 0.21 0.16
[2,] 0.77 1.00 0.50 0.44 0.48 0.28 0.20 0.34 -0.05 -0.09 -0.07 -0.02 -0.01 0.18 0.21
[3,] 0.53 0.50 1.00 0.74 0.91 0.72 0.28 0.79 0.08 -0.03 0.22 0.04 0.06 0.15 0.22
[4,] 0.54 0.44 0.74 1.00 0.82 0.63 0.19 0.56 0.02 -0.07 0.23 0.05 0.10 0.15 0.22
[5,] 0.54 0.48 0.91 0.82 1.00 0.75 0.26 0.70 0.05 -0.08 0.21 0.07 0.09 0.14 0.22
[6,] 0.30 0.28 0.72 0.63 0.75 1.00 0.31 0.63 0.20 0.02 0.27 -0.03 -0.03 -0.01 0.23
[7,] 0.16 0.20 0.28 0.19 0.26 0.31 1.00 0.38 0.03 0.15 0.29 0.23 0.24 0.16 0.29
[8,] 0.36 0.34 0.79 0.56 0.70 0.63 0.38 1.00 0.14 0.05 0.37 0.11 0.11 0.13 0.18
[9,] -0.11 -0.05 0.08 0.02 0.05 0.20 0.03 0.14 1.00 0.50 0.44 -0.04 -0.10 0.13 0.03
[10,] -0.10 -0.09 -0.03 -0.07 -0.08 0.02 0.15 0.05 0.50 1.00 0.62 0.37 0.08 0.11 -0.07
[11,] -0.02 -0.07 0.22 0.23 0.21 0.27 0.29 0.37 0.44 0.62 1.00 0.31 0.21 0.28 0.09
[12,] 0.14 -0.02 0.04 0.05 0.07 -0.03 0.23 0.11 -0.04 0.37 0.31 1.00 0.73 0.12 0.10
[13,] 0.09 -0.01 0.06 0.10 0.09 -0.03 0.24 0.11 -0.10 0.08 0.21 0.73 1.00 0.31 0.32
[14,] 0.21 0.18 0.15 0.15 0.14 -0.01 0.16 0.13 0.13 0.11 0.28 0.12 0.31 1.00 0.41
[15,] 0.16 0.21 0.22 0.22 0.22 0.23 0.29 0.18 0.03 -0.07 0.09 0.10 0.32 0.41 1.00
另一个选项是将图像或图像的 url 传递给http://www.free-ocr.com/,它将尝试解析。它将创建图像的文本文档,然后您可以阅读。