UK Biobank Brain Imaging - Working with Netmats


To reduce file sizes, the square and diagonally-symmetric rfMRI network matrices only have the parts above the diagonal saved out for each subject. To reshape those into the original square matrices requires knowledge of the convention used for this. The below simple matlab code explains how the network matrices ("netmats") are saved out, and how they can be re-formed.

% With a dimensionality of 4, an example original netmat might be:
D=4;
square_net = [0 1 2 4
              1 0 3 5
              2 3 0 6
              4 5 6 0];

% This would be converted into the following row vector for saving to file.
% The upper triangle entries are taken columnwise.
vector_net = square_net(triu(ones(D),1)==1)';
% giving values vector_net = [ 1 2 3 4 5 6 ]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Then  to reverse this process, i.e., read a file back into a square matrix:

D=4;
vector_from_file = % you have to read the vector from file, e.g. downloaded from UK Biobank
% giving vector_from_file = [ 1 2 3 4 5 6 ]

% reshape things into the upper diagonal
square_net_from_file = zeros(D);
square_net_from_file(triu(ones(D),1)>0) = vector_from_file;
% and if you want to fill in the lower diagonal:
square_net_from_file = square_net_from_file + square_net_from_file';