I want to compare different cells in different rows and return a value if conditions are satisfied. 

Suppose the following `s_i` =

```
        [,1]      [,2]      [,3]
[1,] 0.43020494 0.7183179 0.4201009
[2,] 0.08625491 0.3007912 0.8768459
[3,] 0.80012649 0.8448729 0.7131344

```

I want to compare all the rows (pairs), so row 1,2; 1,3 ; 2,3; 2,1; 3,1 and row 3,2 
The output `dgpos` contains the row numbers that are combined and the values returned. 

I want to compare the rows. for the frist to combination or rows 1 and 2

```
1,  if  2b≥1b
0, if 1a≥2c
(1a-2c )/ ((2b-2c) –(1b-1a), otherwise

```

where a, b and c are the columns of s\_i

In R-ish for row 1 and 2 

```
If   (s_i[2,2]>= s_i[1,2])
 dgpos[rowindex,3]=1

If   (s_i[1,1]>= s_i[2,3])
dgpos[rowindex,3]=0

else (otherwise)

dgpos[rowindex,3] =(s_i[1,1]- s_i[2,3])/((s_i[2,2]-s_i[2,3])-(s_i[1,2]-s_i[1,1]))

```

The output I want aim for contains the combinations and the values returned in `dgpos[,3]`

```
    [,1] [,2] [,3]
[1,]    1    2    0.5168453
[2,]    1    3    1
[3,]    2    3    1
[4,]    2    1    1
[5,]    3    1    0
[6,]    3    2    0.1235813

```

I have this:

```
s_i=matrix(runif(9),3)

dgpos=matrix(0,(dim(s_i)[2]*(dim(s_i)[2]-1)),3)

rowindex=1

for (i in 1:nrow(s_i)) {
  for (j in 1:nrow(s_i)) {
    if (i!=j)

      c1=s_i[i,]
    c2=s_i[j+1,]

    dgpos[rowindex,1]=i
    dgpos[rowindex,2]=j+1

    if (c2[2] >= c1[2])
      dgpos[rowindex,3]=1

    dgpos[rowindex,3] = ifelse ((c1[1]=c2[3]), 0 , c1[1]-c2[3]/((c2[2]-c2[3])-(c1[2]-c1[1])))

    rowindex=rowindex+1  
  }
 }

```

I know that loops, are not preferred, but at the moment (my level of r-ish) I don’t know a better solution. I have tried `adply` with `combn`, without result.

MQ: how to compare different cells in different rows and return a value depending on several conditions ?

Your help and commends are appreciated.