Step #1: We are introducing a new axis on the last-column sliced version to keep it as 2D with the last axis being a singleton dimension/axis. The idea is to compare each of its element against all elements in that row except the element itself :