Вы должны попытаться реализовать шаги гауссовского исключения, но вместо того, чтобы удалять элементы i-го столбца на i-м шаге, вы должны попытаться удалить последние элементы.
mymat <- matrix(sample(x = c(1:10),replace = T,size = 25), ncol = 5, nrow = 5, byrow = T)
#iterating from the last row, to the first
for(j in nrow(mymat):1){
if(j>1){
#iterating from first, to the one before the current
for(i in 1:(j-1)){
#subtract the two rows but with the corresponding multiplier to eliminate the column's variables
multiplier <- mymat[i,j]/mymat[j,j]
mymat[i,] <- mymat[i,]-multiplier*mymat[j,]
}
}
#to get 1s in the diagonal line
mymat[j,] <- mymat[j,]/mymat[j,j]
}
Если вы не хотите получать 1 с по диагонали:
for(j in nrow(mymat):1){
if(j>1){
for(i in 1:(j-1)){
multiplier <- mymat[i,j]/mymat[j,j]
mymat[i,] <- mymat[i,]-multiplier*mymat[j,]
}
}
mymat[j,] <- mymat[j,]/mymat[j,j]
}