I have some small symmetric matrices that are low dimensional representations of larger symmetric matrices. I have a vector that is a key showing which cells of the high-D matrix should be linked to which cells in the low-D matrix.
I would like to recreate these larger matrices by populating the larger matrix with its corresponding value in the low dimensional matrix. I believe there should be a vectorized approach to this, but so far all I've been able to come up with is a simple nested for loop, which is prohibitively slow for these matrices (10k+ rows & columns).
In this toy example, key is vec1, low-D matrix is source_mat, and high-D matrix is target_mat. I need to create target_mat where each cell is filled with the corresponding value from source_mat according to the key.
import pandas as pd
import numpy as np
import random
vec1=[]
for x in range (0, 100):
vec1.append(random.randint(0, 19)) #creating the key
vec1=pd.DataFrame(vec1)
sizevec1=vec1.shape[0]
matshape=(sizevec1,sizevec1)
target_mat=np.zeros(matshape) #key and target have same shape
target_mat=pd.DataFrame(target_mat)
temp=np.random.random((20,20))
source_mat=temp*temp.T
for row in range(0,target_mat.shape[0]):
for column in range(0,target_mat.shape[1]):
print 'row is ', row
print 'column is', column
target_mat.iloc[row,column] = source_mat.item(int(vec1.iloc[row]), int(vec1.iloc[column]))