我想知道在Matlab中如何生成一个特定秩(主元列数)的n x n矩阵。我知道您可以使用命令randi(IMAX, m, n)
生成一个m x n的矩阵,其中随机条目介于1和IMAX之间,但是例如,是否可以生成一个4 x 4的矩阵,其中随机条目但只有2个主元列?谢谢。
我意识到由于randi(IMAX, m, n)
形成了一个m x n矩阵,其中包含尽可能多的主元列,因此可以使用以下代码形成一个n x n矩阵A,该矩阵具有k个主元列和1到IMAX之间的随机条目:
A = randi(IMAX, n, k)*randi(IMAX, k, n)
randi(IMAX, n, k)
和 randi(IMAX, k, n)
都只有 k
个主元列,所以它们的乘积也只有 k 个主元列。rand
、randi
还是 randn
生成,都应该可以工作。通常,您可能还会使用相同的矩阵,例如 X = randn(4, 2);
,Y = X * X';
。此外,我编辑了您的答案以包含代码高亮。干杯。 - Colin T Bowers一种(虽然效率低下的)方法可能是生成完整矩阵(在您的示例中为4x4),然后使用SVD分解将其分离,并将某些奇异值归零(在您的情况下为2个条目)。我相信重新组合的矩阵将具有所需的秩。
Matrix_Size=input('Enter a number to generate the desired matrix\n')
Iteration = 2 % This is to run the loop infinitely
while (Iteration>1)
Main_Matrix=sym(randi(7,3,3)) %7 is the range where the elements in my random matrix will occur
Rank_Matrix=rank(Main_Matrix)
if (Rank_Matrix == 2)
break
end
end
Main_Matrix
%random number with specific rank
close all;
clc;
clear;
rng(8);
y= randi(300,3,3);
display(y)
if rank(y)==2
y=y;
else
r = (randi(9)+1);
for i = 1:3
y(3,i)=r*y(1,i);
end
end
%random number with specific rank
%%plotting the figures column space - left null space
figure(1)
plot3([0 CS(1,1)],[0 CS(2,1)],[0 CS(3,1)],'r','linew',3)
hold on
plot3([0 CS(1,2)],[0 CS(2,2)],[0 CS(3,2)],'b','linew',3)
legend('CS - Column 1','CS - Column 2')
hold on
plot3([0 LNS(1,1)],[0 LNS(2,1)],[0 LNS(3,1)],'g','linew',3)
legend('CS - Column 1','CS - Column 2','LNS - Column 1')
title('Column Space - Left Null Space Plot')
hold off
%%matrix decalration and finding the column space, row space and null
%%space and left null space
clear;clc;clear all variables;
B=randi(16,3,2);
C=randi(16,2,3);
A=B*C;
rank(A);
Y=sym(A);
Y
CS = colspace(Y)
RS = colspace(Y')
NS = null(Y)
LNS = null(Y')