# Create custom element-wise operations between two matrices

**URL:** <https://community.konduit.ai/t/create-custom-element-wise-operations-between-two-matrices/1424>\
**Category:** ND4J\
**Created:** [June 4, 2021, 8:21pm UTC](https://community.konduit.ai/t/create-custom-element-wise-operations-between-two-matrices/1424 "2021-06-04T20:21:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wcneill](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/wcneill/32/559_2.png) [@wcneill](https://community.konduit.ai/u/wcneill)\
**Post date:** [June 4, 2021, 8:21pm UTC](https://community.konduit.ai/t/create-custom-element-wise-operations-between-two-matrices/1424/1 "2021-06-04T20:21:52Z")

</div>

I’m trying to figure out how to compute a similarity matrix using a Gaussian similarity score between every data point (row, that is) in an INDArray matrix.

In a for loop, that’d look something like this psuedocode:

```auto
sim_matrix[][]
for row_i in matrix:
    for row_j in matrix:
        if row_i != row_j:
            sim_matrix[i][j] = score(row_i, row_j)

```

Is there anyway to use something like this to get that job done with ND4J:

```auto
Nd4j.getExecutioner().execAndReturn(SimilarityFunction.compute(simMatrix, simMatrix));

```

Thanks!

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [June 4, 2021, 11:56pm UTC](https://community.konduit.ai/t/create-custom-element-wise-operations-between-two-matrices/1424/2 "2021-06-04T23:56:36Z")

</div>

@wcneill we generally impleement those ops as c++ for performance reasons [deeplearning4j/Transforms.java at master · eclipse/deeplearning4j · GitHub](https://github.com/eclipse/deeplearning4j/blob/master/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/ops/transforms/Transforms.java#L91)

we have manhattan, euclidean and cosine similarity. Using the relationship described here:

> <https://datascience.stackexchange.com/questions/17352/why-do-we-use-a-gaussian-kernel-as-a-similarity-metric>

You might be able to get away with manipulating the output from euclidean distance.
