I figured a way but not sure how to vectorize this
            # Indexes to traverse rows
            z_index_1 = []

            # Indexes to traverse cols
             z_index_2 = []

            pointer = 0
            entity_pointer = 0
            
            # num_entities is a 1d tensor that is equivalent to lengths in figure
            for i in range(len(num_entities)):
                for j in range(num_entities[i]):
                    for k in range(num_entities[i]):
                        for l in range(8):
                            z_index_1.append(entity_pointer)
                            z_index_2.append(pointer)
                            pointer += 1
                    entity_pointer += 1

            z_scores_rearranged = torch.zeros(sum(num_entities), sum(num_entities * num_entities) * 8)

            z_scores_rearranged[(z_index_1, z_index_2)] = masked_flattened_scores