Skip to content

mask_to_index

mask_to_index(mask: array) -> array

Converts a boolean mask to the indices of its True entries (the inverse of index_to_mask()).

Parameters:

Name Type Description Default
mask array

A boolean array of shape [size].

required

Returns:

Type Description
array

The sorted int32 indices where mask is True.

Example:

mask_to_index(mx.array([False, True, True, False]))
# mx.array([1, 2])
Source code in mlx_graphs/utils/indexing.py
def mask_to_index(mask: mx.array) -> mx.array:
    """
    Converts a boolean mask to the indices of its ``True`` entries (the
    inverse of :func:`~mlx_graphs.utils.array_ops.index_to_mask`).

    Args:
        mask: A boolean array of shape ``[size]``.

    Returns:
        The sorted int32 indices where ``mask`` is ``True``.

    Example:

    .. code-block:: python

        mask_to_index(mx.array([False, True, True, False]))
        # mx.array([1, 2])
    """
    return mx.array(np.nonzero(np.asarray(mask).reshape(-1))[0].astype(np.int32))

from mlx_graphs.utils import mask_to_index · mlx_graphs/utils/indexing.py