I have an integer variable res
which stores the sum of of each element from one vector to another vector where the results are kept track.
a <- 1:3
b <- 4:6
nm <- outer(seq_along(a), seq_along(b), FUN = function(x, y) sprintf('a%d + b%d', x, y))
res <- setNames(c(outer(a,b,`+`)), nm)
res
# a1 + b1 a2 + b1 a3 + b1 a1 + b2 a2 + b2 a3 + b2 a1 + b3 a2 + b3 a3 + b3
# 5 6 7 6 7 8 7 8 9
How can I find the maximum of each unique pair? Let say a3 + b3 = 9
is the maximum, then in the second iteration, any pair containing a3
or b3
are omitted and we are left with:
res
# a1 + b1 a2 + b1 a1 + b2 a2 + b2
# 5 6 6 7
The next maximum is a2 + b2 = 7
, then in the last iteration, any pair with a2
or b2
are omitted and we are left with:
res
# a1 + b1
# 5
Then we can average the maximum pairing i.e. (9+7+5)/3 = 3
How can I achieve this?