我正在研究奇异值分解10x10的简单矩阵,所有条目都为0。我想在这个矩阵中添加一个硬币翻转并运行SVD。代码如下:
# Simple Example
set.seed(1234)
par(mar=rep(0.2,4))
dataMatrix = matrix(rnorm(100), nrow=10)
dataMatrix
getMatrix = dataMatrix*0
getMatrix
svd1 = svd(getMatrix)
svd1
par(mfrow=c(1,5))
image(t(getMatrix)[,nrow(getMatrix):1], col='blue')
plot(svd1$d,xlab='Column',ylab='Singular Values', pch=19, col='red')
## Add a pattern to the data set , flipping a coin
set.seed(78910)
for (i in 1:40){
coinFlip1 = rbinom(1,size=1, prob=0.5)
coinFlip2 = rbinom(1,size=1,prob=0.5)
if (coinFlip1){
getMatrix[i,]=getMatrix[i,]+rep(c(0,5),each=5)
}
if (coinFlip2){
getMatrix[i,]=getMatrix[i,]+rep(c(0,5),each=5)
}
}
hh = hclust(dist(getMatrix))
getMatrixOrdered= getMatrix[hh$order,]

getMatrix [i,]:下标超出范围
时出错我知道边界有问题,所以我该如何解决?
谢谢!
答案 0 :(得分:0)
for (i in 1:40){
替代
for (i in 1:10){