function affinity_matrix = make_affinity_matrix(seed_branches, target_branches, hemi)

project_dir = pwd;

if upper(hemi)=='R'
    load([project_dir '/data/exemplar_matrix/R_frontal_R_parietal.mat']);
elseif upper(hemi)=='L'
    load([project_dir '/data/exemplar_matrix/L_frontal_L_parietal.mat']);
end

seed_data           = data_hierarchy.correlation;
seed_data_pdist     = pdist(seed_data,'cityblock');
seed_linkage_ward   = linkage(seed_data_pdist,'ward');
seed_idx            = cluster(seed_linkage_ward, 'maxclust', seed_branches);

target_data         = data_hierarchy.correlation';
target_data_pdist   = pdist(target_data,'cityblock');
target_linkage_ward = linkage(target_data_pdist,'ward');
target_idx          = cluster(target_linkage_ward, 'maxclust', target_branches);

data  = data_hierarchy.correlation;

for i_seed = 1:length(unique(seed_idx))
    for i_target = 1:length(unique(target_idx))
        temp = [];
        temp = data(seed_idx==i_seed,target_idx==i_target);
        affinity_matrix(i_seed, i_target) = mean2(temp);
    end
end

cd(['results/largescale_organization/' hemi '_hemisphere/'])

save(['affinity_matrix_' num2str(seed_branches) '_frontal_' num2str(target_branches) '_parietal'], 'affinity_matrix')

end