Note
This page is a reference documentation. It only explains the function signature, and not how to use it. Please refer to the user guide for the big picture.
nilearn.connectome.vec_to_sym_matrix¶
- nilearn.connectome.vec_to_sym_matrix(vec, diagonal=None)[source]¶
Return the symmetric matrix given its flattened lower triangular part.
Acts on the last dimension of the array if not 1-dimensional. Diagonal can be encompassed in vec or given separately. In both cases, note that diagonal elements are multiplied by sqrt(2).
Added in Nilearn 0.3.
- Parameters:
- vecnumpy.ndarray or
listof numpy arrays, shape (…, n_columns * (n_columns + 1) /2) or (…, (n_columns - 1) * n_columns / 2) if diagonal is given separately. The input array.
- diagonalnumpy.ndarray, shape (…, n_columns), default=None
The diagonal array to be stacked to vec. If None, the diagonal is assumed to be included in vec.
- vecnumpy.ndarray or
- Returns:
- symnumpy.ndarray, shape (…, n_columns, n_columns).
The output symmetric matrix.
See also
Notes
This function is meant to be the inverse of sym_matrix_to_vec. If you have discarded the diagonal in sym_matrix_to_vec, you need to provide it separately to reconstruct the symmetric matrix. For instance this can be useful for correlation matrices for which we know the diagonal is 1.
Examples
Create a vector representing the flattened lower triangular part (including the diagonal) of a symmetric matrix >>> import numpy as np >>> vec = np.arange(1, 7)
>>> from nilearn.connectome import vec_to_sym_matrix >>> sym = vec_to_sym_matrix(vec) >>> sym array([[1.41421356, 2. , 4. ], [2. , 4.24264069, 5. ], [4. , 5. , 8.48528137]])