0

I have this code:

for(b in seq(nrow(A), 1, -1)){
result[nrow(A)-b+1]<-A[b,ncol(A)]/A[b,b]
for(t in seq(b,(nrow(A)-1), 1)){
  result[nrow(A)-b+1]<- result[nrow(A)-b+1]-(result[t,b]/A[b,b])

and getting this errorError in seq.default(b, (nrow(A) - 1), 1) : wrong sign in 'by' argument My idea is, that second cycle in first step of first cycle doesnt run, in second step once, in third step twice... What is wrong? It thinks maybe I wrote it bad, because in first step i have "start number" bigger by one then "end number" and with the sign of "by number" this cycle can not reach the end number. But I need it in this way, i assumed that it in that case it just jump over that cycle... I know I can do it with some condition before cycle, but I want to know core of the problem. Thank you very much, Martin! Whole code:

Gaus<-function(A){
for(i in seq(1, nrow(A)-1, 1)){
for(n in seq(i, nrow(A)-1, 1)){
  multiplier<-A[n+1,i]/A[i,i]
  if(multiplier!=0)
    for(ch in seq(i, ncol(A), 1)){
      print(ch)
      A[n+1,ch]<-A[n+1,ch]-(A[i,ch]*multiplier)
      print(A)

    }
}

}

 result <-c()
for(b in seq(nrow(A), 1, -1)){
result[nrow(A)-b+1]<-A[b,ncol(A)]/A[b,b]
for(t in b:(nrow(A)-1)){
  result[nrow(A)-b+1]<- result[nrow(A)-b+1]-(result[t,b]/A[b,b])
}

} }

A <- matrix(c(2,4,5,8,3,2,8,7,9,1,6,5), 3, 4, TRUE)
Gaus(A)`
  • Please show some example and expected output based on that. – akrun Oct 17 '15 at 09:59
  • A <- matrix(c(2,4,5,8,3,2,8,7,9,1,6,5), 3, 4, TRUE) – Martin Vlasák Oct 17 '15 at 10:13
  • `seq` returns this error if `to` is larger than `from` and `by` is negative, or the other way around: `seq(0, 1, -1); seq(0, -1, 1)`. Find out why that happens in your code. – Roland Oct 17 '15 at 10:14
  • i changed it to for(t in b:(nrow(A)-1)) but now it says Error in result[t, b] : incorrect number of dimensions – Martin Vlasák Oct 17 '15 at 10:16
  • `result` is undefined and the closing brace brackets are missing and there may be other problems too but those prevent the code from being run even if one uses the `A` in your comment. Please provide complete self-contained code with no syntax errors when asking questions on SO. http://stackoverflow.com/help/mcve http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – G. Grothendieck Oct 17 '15 at 10:52
  • On the first pass through the outer loop `b` will be more than `nrow(A)-1` and the "direction" implied will not match the increment in the second call to `seq`. I think the body of your question is saying that you expect the `seq` function to automagically correct your errors in logic. The strangeness I fear is not in `seq` but in your mind. – IRTFM Oct 17 '15 at 16:40
  • Try this code, and you will see what is the problem with for cycle... if n is 5, so then i become 5 and condition is to 4(I expect, it will just skip then) then it print(k) anyway...... for(n in seq(5, 1, -1)){ print(n) for(i in n:4){ print("k") print(n) } } – Martin Vlasák Oct 18 '15 at 10:50

0 Answers0