def filter_vec_by_graph(G, vec, nodelist):
  """
  Return a subset of vec and nodelist corresponding to the nodes defined in G
  """
  inds = []
  nodelist_sub = []
  for i, node in enumerate(nodelist):
    if node in G:
      inds.append(i)
      nodelist_sub.append(node)
  vec_sub = vec[inds]
  return vec_sub, nodelist_sub