Mask lower triangle of a Representational Dissimilarity Matrix (RDM)

This recipe takes a Representational Dissimilarity Matrix (RDM) as a square numpy array, masks the diagonal and lower triangle, and outputs a flattened numpy array of the upper triangle.

Easily adaptable for Representational Similarity Analysis (RSA), Functional/Structural Connectivity analyses, or other analyses with related pipelines with symmetric, square matrices.

Requirements:

  • numpy>=1.18.1
1
2
3
4
5
6
7
def flattenRDM(square_matrix):
    assert square_matrix.shape[0] == square_matrix.shape[1], "Must be a square numpy array"
    
    # mask the diagonal and lower triangle and output flattened array
    flattened_out = square_matrix[np.triu_indices(len(square_matrix), k=1)] 

    return flattened_out

Example usage:

1
2
3
4
5
6
7
8
import numpy as np

square_matrix = np.array([[1,0,0,0],
                          [0,1,0,0],
                          [0,0,1,0],
                          [0,0,0,1]])

flattenRDM(square_matrix)

Recipe made by Shawn Rhoads

These recipes may also be of interest