MATLAB: Mean of indexed data in a matrix

indexmatrices

Hi all
I would appreciate your help. I am working with a 450*450 correlation matrix, which has correlations between variables that belong to 5 classes. Assuming I have a vector of length 1*450 with labels for each of the rows and columns in my matrix, what would be the easiest way to calculate the mean of correlations for variables that share the same label, and those that have different labels, ignoring same-same correlations? I do not need to calculate anything for any of the specific labels, but rather just one mean for variables that have the same labels, and another for those that have different labels
So, for example, given the following label vector [1 1 2 2 1]
and the following matrix:
1 0.008 0.4168 0.3166 0.3269;
0.008 1 0.4894 0.8251 0.9821;
0.4168 0.4894 1 0.8132 0.6861;
0.3166 0.8251 0.8132 1 0.4929;
0.3269 0.9821 0.6861 0.4929 1
The within-labels mean would be of cells:
0.008
0.3269
0.9821
0.8132
And the between-label mean would be of cells:
0.4168
0.3166
0.4894
0.8251
0.6861
0.4929

Best Answer

Here it is in just 5 lines (which could be reduced further)
% Your data
data = [1 0.008 0.4168 0.3166 0.3269; 0.008 1 0.4894 0.8251 0.9821; 0.4168 0.4894 1 0.8132 0.6861; 0.3166 0.8251 0.8132 1 0.4929; 0.3269 0.9821 0.6861 0.4929 1];
groups = [ 1 1 2 2 1];
% Create within-groups index matrix
groupMat = groups == groups';
% Isolate the lower half of the correlation matrix
% replacing up half and identity line with NaN
dataTrim = data;
dataTrim(triu(true(size(data)))) = NaN;
% Extract within-group and between-group correlations
% and calculate mean ignoring NaN values.
withinGroupMean = nanmean(dataTrim(groupMat));
betweenGroupMean = nanmean(dataTrim(~groupMat));