For example, np.array([[1,2],[3,4]])[np.triu_indices(2)]
has shape (3,)
, being a flattened list of the upper triangular entries. However, if I have a batch of 2x2 matrices:
foo = np.repeat(np.array([[[1,2],[3,4]]]), 30, axis=0)
and I want to obtain the upper triangular indices of each matrix, the naive thing to try would be:
foo[:,np.triu_indices(2)]
However, this object is actually of shape (30,2,3,2)
(as opposed to (30,3)
that we might expect if we had extracted the upper triangular entries batch-wise.
How can we broadcast tuple indexing along the batch dimensions?