Randomise the upper triangle of a matrix

5 visualizzazioni (ultimi 30 giorni)
Kyle il 21 Ago 2014
Modificato: Kyle il 22 Ago 2014
How can I randomise the elements only in the upper triangle of a square matrix? I want to randomise only the upper triangle elements 10,000 times, each time the upper triangle is different. Please suggest a solution.
Many thanks
  1 Commento
Kyle il 21 Ago 2014
Modificato: Kyle il 22 Ago 2014
Thanks all experts providing solution to my concern. Especially thanks to Roger Stafford. Roger's answer invokes a solution to randomise the upper-triangle element locations:
M = magic(5)
up_ind = find(triu(ones(size(M,1)),1)==1);
up_length = length(up_ind);
up_elements = M(up_ind);
for k = 1: 5
M(up_ind) = up_elements(randperm(up_length) );
out(:,:,k) = M;
So the 5 generated matrix will have the same lower-triangle but different upper triangles in which elements' positions are randomised.

Accedi per commentare.

Risposta accettata

Roger Stafford
Roger Stafford il 21 Ago 2014
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:
M(f) = rand(n2,1); % Or use randn or any other random number function
You accomplish the randomization in one line and with only n*(n-1)/2 calls on the random number function.
  4 Commenti
Sean de Wolski
Sean de Wolski il 21 Ago 2014
Ahh, I see what you're saying.

Accedi per commentare.

Più risposte (2)

Guillaume il 21 Ago 2014
a(logical(triu(ones(size(a))))) = rand(1, sum(sum(triu(ones(size(a))))));
The sum(sum expression is just to find how many random numbers to generate.

Patrik Ek
Patrik Ek il 21 Ago 2014
Modificato: Patrik Ek il 21 Ago 2014
Try the triu function that extracts the upper triangular part of a matrix.
b = ones(10);
a = randn(10);
aUpper = triu(a);
b(aUpper ~= 0) = aUpper(aUpper~=0); % To replace the upper elements in b.
Another solution is:
b = ones(10);
bL = tril(b);
a = randn(10);
aU = triu(a);
c = bL+aU;
I think I would do it as in the first example, but if you want you can try both and select the one you want. Of course you will need to create some kind of loop and do some stuff to save the matrices (or may use them and discard them, or whatever you need). However, from here you should be able to handle it by yourself.
Good luck!
  2 Commenti
Patrik Ek
Patrik Ek il 21 Ago 2014
The purpose were to show a way to create a random upper triangular matrix. The guess was that you would be able to finish the script by yourself then. Have you tried to solve the problem yourself? Ok I will update my answer.

Accedi per commentare.


Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by