user123
user123

Reputation: 33

Generating random matrix in MATLAB

I would like to generate 100 random matrices A=[a_{ij}] of size 6 by 6 in (0, 9) using matlab programming satisfying the following properties:

 1. multiplicative inverse:      i.e., a_{ij}=1/a_{ji}      for all i,j=1,2,...,6.
 2. all entries are positive:    i.e., a_{ij}>0             for all i,j=1,2,...,6.
 3. all diagonal elements are 1: i.e., a_{ii}=1             for all i=1,2,..,6.
 4. transitive:                  i.e., a_{ih}*a_{hj}=a_{ij} for all i,j,h=1,2,...,6.

So far, I tried to use a matlab function rand(6)*9. But, I got wrong matrices. I was wondering if anyone could help me?

Here is my matlab code:

clc; clear;
n=6;
m=0;
for i=1:n
    for j=1:n
        for h=1:n
            while m<100  % generate 100 random matrices
                A=rand(n)*9;           % random matrix in (0,9)
                A(i,j)>0;              % positive entries
                A(i,j)==1/A(j,i);      % multiplicative inverse
                A(i,h)*A(h,j)==A(i,j); % transitive
                if i==j && j==h
                    A(i,j)==1;         % diagonal elements are 1
                break;
            end
            m=m+1;
            M{m}=A
        end
    end
end
end
M{:}      

Upvotes: 0

Views: 446

Answers (2)

Burak
Burak

Reputation: 2495

clear; clc
M = cell(1, 100); % preallocate memory
% matrix contains both x & 1/x
% we need a distribution whose multiplication with its inverse is uniform
pd = makedist('Triangular', 'a', 0, 'b', 1, 'c', 1);
for m=1:100 % 100 random matrices
    A = zeros(6); % allocate memory
    % 5 random numbers for 6x6 transitive random matrix
    a = random(pd, 1, 5);
    % choose a or 1/a randomly
    ac = rand(1, 5) < 0.5;
    % put these numbers above the diagonal
    for i=1:5
        if ac(i)
            A(i, i+1) = a(i);
        else
            A(i, i+1) = 1 / a(i);
        end
    end
    % complete the transitivity going above
    for k=flip(1:4)
        for i=1:k
            A(i, i-k+6) = A(i, i-k+5) * A(i-k+5, i-k+6);
        end
    end
    % lower triangle is multiplicative inverse of upper triangle
    for i=2:6
        for j=1:i-1
            A(i,j) = 1 / A(j,i);
        end
    end
    c = random(pd); % triangular random variable between (0,1)
    A = A ./ max(A(:)) * 9 * c; % range becomes (0, 9*c)
    % diagonals are 1
    for i=1:6
        A(i,i) = 1;
    end
    % insert the result
    M{m} = A;
end

There are actually 5 numbers are independent in 6x6 transitive matrix. The others are derived from them as shown in the code.

The reason why triangular distribution is used for these numbers is because pdf of triangular distribution is f(x)=x, and pdf of inverse triangular distribution is f-1(x)=1/x; thus their multiplication becomes uniform distribution. (See pdf of inverse distribution)

A = A ./ max(A(:)) * 9; makes the range (0,9), but there will always be 9 as the maximum element. We need to shrink the result by a random coefficient to obtain the result uniformly distributed in (0,9). Since A is uniformly distributed, we can achieve this again by triangular distribution. (See product distribution)

Another solution to the range issue would be calculating A while its maximum is above 9. This would eliminate the latter problem.

Since all elements of A depends on 5 random variables, the distribution of them will never be perfectly uniform, but the aim here is to maintain a reasonable scale for them.

Upvotes: 2

Daniel
Daniel

Reputation: 36710

It took me a litte to think about your question, but I realized there is no solution.

You require your elements of A to be uniformly distributed in the (0,9) range. You also require a_{ij}*a_{jk}=a_{ik}. Since the product of two uniform distributions is not a unifrom distribution, there is no solution to your question.

Upvotes: 2

Related Questions