For anyone finding this and wondering about the perms solution... see below:
MATLAB CODE
ncond=5;
M=rand(ncond);
possible_selections=perms(1:ncond);
for jj=1:size(possible_selections,1)
s(jj)=sum(M(mat2vec([possible_selections(jj,:)' [1:ncond]'],[ncond ncond])));
end
[~,best_selection_index]=min(s);
best_selection=possible_selections(best_selection_index,:);
% i.e., best_selection(1)= row to pick from column 1
% best_selection(2)= row to pick from column 2 ...
This requires the following function which is my homebaked version of something I am sure can be done in a much easier way. It outputs the index values of a matrix if you know the row and column values and size of the matrix, or vice versa.
MATLAB CODE
function [out] = mat2vec(rowcol,msize);
if size(rowcol,2) == 2
out = (rowcol(:,2)*msize(1))-(msize(1)-rowcol(:,1));
else
out = zeros(length(rowcol),2);
fl= floor(rowcol/msize(1));
o1=rowcol/msize(1)-fl;
out(find(o1 == 0),2) = fl(find(o1 == 0));
out(find(o1 == 0),1) = msize(1);
out(find(o1 ~= 0),2) = fl(find(o1 ~= 0))+1;
out(find(o1 ~= 0),1) = round(o1(find(o1 ~= 0)).*msize(1));
end