Randomise the upper triangle of a matrix

5 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(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 中查找有关 Linear Algebra 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by