如何创建一个函数来减少R中的矩阵?

如何创建一个函数来减少R中的矩阵?,r,R,到目前为止,我已经尝试了以下代码,但在R-studio中不起作用;它就挂在那里 我做错什么了吗?这是我的第一个真正的R代码项目,所以我喜欢的建议 new.rref <- function(M,fractions=FALSE) { #M is a matrix. #Require numeric matricies. if ((!is.matrix(M)) || (!is.numeric(M))) stop("Sorry pal! Data not a numeric matrix.")

到目前为止,我已经尝试了以下代码,但在R-studio中不起作用;它就挂在那里

我做错什么了吗?这是我的第一个真正的R代码项目,所以我喜欢的建议

new.rref <- function(M,fractions=FALSE)
{
#M is a matrix. 
#Require numeric matricies. 
if ((!is.matrix(M)) || (!is.numeric(M)))
stop("Sorry pal! Data not a numeric matrix.")
#Specify and differentiate between rows and columns.
r=nrow(M)
c=ncol(M)
#Now establish a continuous loop (*needed help on this one)
#According to the help documents I've read, this has to do with a
#computerized version of the Gaussian Reducing Algorithm
#While 1<r and 1<c, must set first column entries in which
#1:r < 1 equal to zero. This while loop is used to loop the
#algorithm until a specific condition is met -- in this case,
#until elements in the first column to which 1:r < 1
#are set to zero. 
while((1<=r) & (1<=c))
new <- M[,1]
new[1:r < y.position] <- 0
# Now here's the fun part :)
#We need to find the maximum leading coefficient that lies
#at or below the current row. 
new1 <- which.max(abs(new))
#We will assign these values to the vector "LC"
LC <- col[which]
#Now we need to allow for row exchange!
#Basically tells R that M[c(A,B),] = M[c(B,A),].
if (which > 1) { M[c(1,which),]<-A[c(which,1),] }
#Now we have to allow for the pivot, "sweep", and restoration
#of current row. I totally didn't know how to do this so I
#used and changed some code from different documentations.
#PIVOT (friends reference)
M[1,]<-M[1,]/LC 
new2 <-M[1,]
#CLEAN
M <- M - outer(M[,x.position],new2) 
#RESTORE
A[1,]<-new2 
#Last, but certantly not least, we're going to round the matrix
#off to a certain value. I might have did this wrong.  
round(M)
return(M)
print(M)
}

new.rref当然!输入将是任何带有整数项(无分数)的矩阵M,输出将是一行缩减矩阵。好的!假设你有一个矩阵
M=matrix(c(2,3,4,7),nrow=2,ncol=2,byrow=TRUE)
new.rref(M)
需要生成矩阵MSure的行缩减形式!:我刚上传了它的图片!输入将是任何带有整数项(无分数)的矩阵M,输出将是一行缩减矩阵。好的!假设你有一个矩阵
M=matrix(c(2,3,4,7),nrow=2,ncol=2,byrow=TRUE)
new.rref(M)
需要生成矩阵MSure的行缩减形式!:我刚上传的