Outer function in R for upper triangular matrix

Viewed 1145

I have this code currently

s1=seq(0,10,length.out=3)
s2=seq(0,10,length.out=3)
d=outer(s1,s2,`-`)
I=outer(s1,s2,`==`)

However I only want the upper triangular of d and I so I am currently doing

d=d[upper.tri(d,diag=T)]
I=I[upper.tri(I,diag=T)]

Is there a way to make this code faster through incorporating the upper triangular matrix in the outer function? Note that this is a reproducible example, however my code is much bigger and I need to decrease the running time as much as possible.

1 Answers
Related