If you are going to do this randomization 10000 times, you would want it to be as efficient as possible. Here is a possible way. Let the matrix be of size n x n. (Note that I am assuming by the upper triangular part of a matrix you mean the part above the main diagonal but not including the diagonal.)
Do this just once and save f and n2:
f = find(triu(ones(n),1)==1);
n2 = length(f);
Then each of the 10000 times if your n x n matrix is M, do this:
You accomplish the randomization in one line and with only n*(n-1)/2 calls on the random number function.
Best Answer