在 Matlab 中生成随机矩阵
Generate Random Matrix in Matlab
有没有办法在 Matlab 中生成一个 5000 x 1000 的随机数矩阵,其中:
MM = betarnd(A,B,1,1000);
但 A 和 B 是向量 (1 x 5000)。我收到以下错误消息:
??? Error using ==> betarnd at 29
Size information is inconsistent.
我想避免像下面这样的循环:
for ii = 1 : 1000
MM(:,ii) = betarnd(A,B);
end
谢谢!
您可以重复 A
和 B
(大小为 1x5000 的向量)以获得大小为 1000x5000 的矩阵,其中所有行都相等,并将这些矩阵用作 betarnd
的输入.这样你会得到大小为 1000x5000 的结果,其中列 k
包含 1000 个随机值,参数为 A(k)
和 B(k)
.
原因是,根据documentation(强调我的):
R = betarnd(A,B)
returns an array of random numbers chosen from the
beta distribution with parameters A
and B
. The size of R
is the common size of A
and B
if both are arrays.
所以,使用
MM = betarnd(repmat(A(:).',1000,1), repmat(B(:).',1000,1));
有没有办法在 Matlab 中生成一个 5000 x 1000 的随机数矩阵,其中:
MM = betarnd(A,B,1,1000);
但 A 和 B 是向量 (1 x 5000)。我收到以下错误消息:
??? Error using ==> betarnd at 29
Size information is inconsistent.
我想避免像下面这样的循环:
for ii = 1 : 1000
MM(:,ii) = betarnd(A,B);
end
谢谢!
您可以重复 A
和 B
(大小为 1x5000 的向量)以获得大小为 1000x5000 的矩阵,其中所有行都相等,并将这些矩阵用作 betarnd
的输入.这样你会得到大小为 1000x5000 的结果,其中列 k
包含 1000 个随机值,参数为 A(k)
和 B(k)
.
原因是,根据documentation(强调我的):
R = betarnd(A,B)
returns an array of random numbers chosen from the beta distribution with parametersA
andB
. The size ofR
is the common size ofA
andB
if both are arrays.
所以,使用
MM = betarnd(repmat(A(:).',1000,1), repmat(B(:).',1000,1));