# for loop in R to print iterations

I am trying to code an update rule using functions in R. I have a set of 5 points in coordinate space. I first create a kernel matrix out of it and then calculate the vector norm of it. Now I want to possibly make use of a function call and do a rule update as described below:

I have the following code:

``````library(tidyverse)

dm <- tribble(
~x, ~y,
-0.5, 1,
-1, -1.5,
-1.5, 1.5,
1.5, -0.5,
0.5, -0.5,
) %>% as.matrix()

print("Kernel matrix")
gram <- (dm %*% t(dm))^2   #inner dot product

a <- c(-0.5,1)
b <- c(-1,-1.5)
c <- c(-1.5,1.5)
d <- c(1.5,-0.5)
e <- c(0.5,-0.5)
df <- data.frame(a,b,c,d,e)

print("Max norm")
norm <- max(sqrt(colSums(df^2)))^2 #max norm

b = 0
alpha = replicate(5, 0)
y = c(-1, 1, 1, 1, -1)

cat('---Iteration---', '---alpha vec---','---b---')

for (k in seq(0:4)){
for (i in seq(0:4)){
s = 0
for (j in seq(0:4)){
s = s + (alpha[j] * y[j] * gram[j][i])

s = s + b
s = s * y[i]

if (s <= 0){
alpha[i] = alpha[i] + 1
b = b + (y[i] * norm)
}
cat(i+1, '---',alpha, '---', b)

}
}
}
``````

I want to update the condition only then the value drops below 0 or equals 0 for the set of points.

I seem it little difficult to proceed with the looping in R.

Below is the expected output:

``````1  ==>  [1. 0. 0. 0. 0.]  ==>  -4.499999999999999
2  ==>  [1. 1. 0. 0. 0.]  ==>  0.0
3  ==>  [1. 1. 1. 0. 0.]  ==>  4.499999999999999
4  ==>  [1. 1. 1. 0. 0.]  ==>  4.499999999999999
5  ==>  [1. 1. 1. 0. 1.]  ==>  0.0

1  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
2  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
3  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
4  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
5  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999

1  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
2  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
3  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
4  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
5  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999

1  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
2  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
3  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
4  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
5  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999

1  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
2  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
3  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
4  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
5  ==>  [2. 1. 1. 0. 1.]  ==>  -4.499999999999999
``````

I tried for the first row but not able to proceed further.

Also, I am getting an error:

``````2 --- 1 0 0 0 0 --- -4.52 --- 1 0 0 0 0 --- -4.52 --- 2 0 0 0 0 --- -92 --- 2 0 0 0 0 --- -9Error in if (s <= 0) { : missing value where TRUE/FALSE needed
>
``````