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 Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by