def map2matrix(data_map, layout):
    r"""Map to Matrix

    This method transforms a 2D map to a 2D matrix

    Parameters
    ----------
    data_map : np.ndarray
        Input data map, 2D array
    layout : tuple
        2D layout of 2D images

    Returns
    -------
    np.ndarray 2D matrix

    Raises
    ------
    ValueError
        For invalid layout

    Examples
    --------
    >>> from modopt.base.transform import map2matrix
    >>> a = np.array([[0, 1, 4, 5], [2, 3, 6, 7], [8, 9, 12, 13],
    [10, 11, 14, 15]])
    >>> map2matrix(a, (2, 2))
    array([[ 0,  4,  8, 12],
           [ 1,  5,  9, 13],
           [ 2,  6, 10, 14],
           [ 3,  7, 11, 15]])

    """

    layout = np.array(layout)

    # Select n objects
    n_obj = np.prod(layout)

    # Get the shape of the images
    image_shape = (np.array(data_map.shape) // layout)[0]

    # Stack objects from map
    data_matrix = []

    for i in range(n_obj):
        lower = (image_shape * (i // layout[1]),
                 image_shape * (i % layout[1]))
        upper = (image_shape * (i // layout[1] + 1),
                 image_shape * (i % layout[1] + 1))
        data_matrix.append((data_map[lower[0]:upper[0],
                            lower[1]:upper[1]]).reshape(image_shape ** 2))

    return np.array(data_matrix).T
r"""Map to Matrix

    This method transforms a 2D map to a 2D matrix

    Parameters
    ----------
    data_map : np.ndarray
        Input data map, 2D array
    layout : tuple
        2D layout of 2D images

    Returns
    -------
    np.ndarray 2D matrix

    Raises
    ------
    ValueError
        For invalid layout

    Examples
    --------
    >>> from modopt.base.transform import map2matrix
    >>> a = np.array([[0, 1, 4, 5], [2, 3, 6, 7], [8, 9, 12, 13],
    [10, 11, 14, 15]])
    >>> map2matrix(a, (2, 2))
    array([[ 0,  4,  8, 12],
           [ 1,  5,  9, 13],
           [ 2,  6, 10, 14],
           [ 3,  7, 11, 15]])