Randomise the upper triangle of a matrix

7 visualizaciones (últimos 30 días)
Kyle
Kyle el 21 de Ag. de 2014
Editada: Kyle el 22 de Ag. de 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
Kyle
  1 comentario
Kyle
Kyle el 21 de Ag. de 2014
Editada: Kyle el 22 de Ag. de 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;
end
So the 5 generated matrix will have the same lower-triangle but different upper triangles in which elements' positions are randomised.

Iniciar sesión para comentar.

Respuesta aceptada

Roger Stafford
Roger Stafford el 21 de Ag. de 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 comentarios
Roger Stafford
Roger Stafford el 21 de Ag. de 2014
Kyle needs 'f' to address the location in M to be randomized. Also the n2 value here is too large. It would be n^2*(n^2-1)/2.
Sean de Wolski
Sean de Wolski el 21 de Ag. de 2014
Ahh, I see what you're saying.

Iniciar sesión para comentar.

Más respuestas (2)

Guillaume
Guillaume el 21 de Ag. de 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 el 21 de Ag. de 2014
Editada: Patrik Ek el 21 de Ag. de 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 comentarios
Kyle
Kyle el 21 de Ag. de 2014
Dear Patrik, your method will randomise the whole matrix. What my attempt is to fix the lower triangle of the matrix while manipulating the upper triangle.
Patrik Ek
Patrik Ek el 21 de Ag. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by