# Combine the permutation/combination of two tensors in new tensor

First up, apologies for the confusing title… as for lack of a better wording, ill explain in numbers:

I have given two tensors
t1 = [[1,2,3], [2,3,4]]
t2 = [[5,6],[6,7],[7,8]]

further i have a function f(x,y) that takes one element of each tensor (e.g. t1 and t2) and outputs some value.

What I now aim for, is an efficient way of building a new tensor t3, such that t3[0,1] = f(t1, t2)

[[f([1,2,3],[5,6]), f([1,2,3],[6,7]), f([1,2,3],[7,8])],
[f([2,3,4],[5,6]), f([2,3,4],[6,7]), f([2,3,4],[7,8])]]

Naturally I can solve this via a for loop but I have the feeling that there is a cool tensor magic way.

``````t3 = torch.zeros((len(t1), len(t2)))
for i in range((len(t1)):
for j in range(len(t2)):
t3[i,j] = f(t1[i], t2[j])
``````

I think it would already be a huge help to get to know a way to just have t3 containing the raw values of t1 and t2 at the appropriate position. (I guess the function might then be easily applicable later on)

Is the function arbitarary or is it fixed?

the function is fixed and returns a scalar value.

It is imperative that you first rewrite the function to take in t1 and t2 and give a tensor as an output rather than a scalar. Can you print out the function?

The actual function is rather complex but for the sake of this example we might assume it to be something like

``````def f(x,y):