Numpy Notes

Compute dot product of multidimensional matrix with vector

Supposed we have an (N, N, N, 3) matrix representing the (X, Y, Z) coordinates of all voxels in a rectangular 3D grid.

In order to compute the dot product of each voxel coordinate with a vector, we can do:

vector = np.array((1, 0, 0))
np.tensordot(grid, vector, axes=1)

Update Xarray DataArray Based On Condition

In numpy, you can do this

>>> xxx[condition] = 123

This throws an error on Xarray DataArray

IndexError: Unlabeled multi-dimensional array cannot be used for indexing: x

Instead, do

>>> xxx = xxx.where(~condition, 123)