def reorderMAT(m, H=5000, cost='line'):
    '''
    This function reorders the connectivity matrix in order to place more
    edges closer to the diagonal. This often helps in displaying community
    structure, clusters, etc.

    Parameters
    ----------
    MAT : NxN np.ndarray
        connection matrix
    H : int
        number of reordering attempts
    cost : str
        'line' or 'circ' for shape of lattice (linear or ring lattice).
        Default is linear lattice.

    Returns
    -------
    MATreordered : NxN np.ndarray
        reordered connection matrix
    MATindices : Nx1 np.ndarray
        reordered indices
    MATcost : float
        objective function cost of reordered matrix

    Notes
    -----
    I'm not 100% sure how the algorithms between this and reorder_matrix
    differ, but this code looks a ton sketchier and might have had some minor
    bugs in it.  Considering reorder_matrix() does the same thing using a well
    vetted simulated annealing algorithm, just use that. ~rlaplant
    '''
    from scipy import linalg, stats
    m = m.copy()
    n = len(m)
    np.fill_diagonal(m, 0)

    # generate cost function
    if cost == 'line':
        profile = stats.norm.pdf(range(1, n + 1), 0, n / 2)[::-1]
    elif cost == 'circ':
        profile = stats.norm.pdf(range(1, n + 1), n / 2, n / 4)[::-1]
    else:
        raise BCTParamError('dfun must be line or circ')
    costf = linalg.toeplitz(profile, r=profile)

    lowcost = np.sum(costf * m)

    # keep track of starting configuration
    m_start = m.copy()
    starta = np.arange(n)
    # reorder
    for h in range(H):
        a = np.arange(n)
        # choose two positions and flip them
        r1, r2 = rng.randint(n, size=(2,))
        a[r1] = r2
        a[r2] = r1
        costnew = np.sum((m[np.ix_(a, a)]) * costf)
        # if this reduced the overall cost
        if costnew < lowcost:
            m = m[np.ix_(a, a)]
            r2_swap = starta[r2]
            r1_swap = starta[r1]
            starta[r1] = r2_swap
            starta[r2] = r1_swap
            lowcost = costnew

    M_reordered = m_start[np.ix_(starta, starta)]
    m_indices = starta
    cost = lowcost
    return M_reordered, m_indices, cost
This function reorders the connectivity matrix in order to place more
    edges closer to the diagonal. This often helps in displaying community
    structure, clusters, etc.

    Parameters
    ----------
    MAT : NxN np.ndarray
        connection matrix
    H : int
        number of reordering attempts
    cost : str
        'line' or 'circ' for shape of lattice (linear or ring lattice).
        Default is linear lattice.

    Returns
    -------
    MATreordered : NxN np.ndarray
        reordered connection matrix
    MATindices : Nx1 np.ndarray
        reordered indices
    MATcost : float
        objective function cost of reordered matrix

    Notes
    -----
    I'm not 100% sure how the algorithms between this and reorder_matrix
    differ, but this code looks a ton sketchier and might have had some minor
    bugs in it.  Considering reorder_matrix() does the same thing using a well
    vetted simulated annealing algorithm, just use that. ~rlaplant