Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

How to create a mxn matrix with a specific rank in matlab?

I want to create a m by n matrix with rank k.

Like A is 8 × 8 with rank 5 or B is 4 × 6 with rank 4.

So I try to write a function in MATLAB like below.

My thought is:

  1. generate an m by n zeros matrix
  2. generate m by n matrix and convert it into reduced row echelon form
  3. assign rank of 2.'s matrix to num
  4. if num = k, then assign current matrix to the output
  5. break the iteration
function output = check_rank(m,n,k)
    while 1
        output = zeros(m,n);
        matrix = randi(20,m,n);
        tmp = rref(matrix);
        num = rank(tmp);
        if (num == k)
            output = matrix;
            break;
    end
    disp(output);    
end
A = check_rank(8,8,4)

The outcome is an infinite loop and all the answers are 6x6 zeros matrix: Command Window Output


I have also tried method in the how to create a rank k matrix using matlab?

A = zeros(8,8);
for i = 1:4, A = A + randn(8,1) * randn(1,8); end
A
rank(A)

It can reach my goal, but I have no idea how it work successfully?

Thanks, @anonymous!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to generate a random matrix with specified rank, you can try to build a user function like below

function [Y,rk] = fn(m,n,k)
  P = orth(randn(m,k));
  Q = orth(randn(n,k))';
  Y = P*Q;
  rk = rank(Y);
end

where P and Q are unitary matrices. Y is the generated matrix with random values, and rk helps you check the rank.


Example

>> [Y,rk] = fn(8,6,5)
Y =

   3.8613e-02   7.5837e-03  -7.1011e-02  -7.0392e-02  -3.8519e-02   1.6612e-01
  -3.1381e-02  -3.6287e-02   1.4888e-01  -7.6202e-02  -3.7867e-02   3.2707e-01
  -1.9689e-01   2.2684e-01   1.2606e-01  -1.2657e-03   1.9724e-01   7.2793e-02
  -1.2652e-01   7.7531e-02   1.3906e-01   3.1568e-02   1.8327e-01  -1.3804e-01
  -2.6604e-01  -1.4345e-01   1.6961e-03  -9.7833e-02   5.9299e-01  -1.5765e-01
   1.7787e-01  -3.5007e-01   3.8482e-01  -6.0741e-02  -2.1415e-02  -2.4317e-01
   8.9910e-02  -2.5538e-01  -1.8029e-01  -7.0032e-02  -1.0739e-01   2.2188e-01
  -3.4824e-01   3.7603e-01   2.8561e-02   2.6553e-02   2.4871e-02   6.8021e-01

rk = 5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...