Randomise the upper triangle of a matrix

조회 수: 7 (최근 30일)
Kyle
Kyle 2014년 8월 21일
편집: Kyle 2014년 8월 22일
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
Kyle
Kyle 2014년 8월 21일
편집: Kyle 2014년 8월 22일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Roger Stafford
Roger Stafford 2014년 8월 21일
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
Roger Stafford
Roger Stafford 2014년 8월 21일
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 2014년 8월 21일
Ahh, I see what you're saying.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Guillaume
Guillaume 2014년 8월 21일
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 2014년 8월 21일
편집: Patrik Ek 2014년 8월 21일
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
Kyle
Kyle 2014년 8월 21일
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 2014년 8월 21일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by