Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Random Matrix in Matlab

Tags:

random

matlab

Is there any way in Matlab to generate a 5000 x 1000 matrix of random numbers in which:

MM = betarnd(A,B,1,1000);

but A and B are vectors (1 x 5000). I get the following error message:

??? Error using ==> betarnd at 29
Size information is inconsistent.

I want to avoid a loop like the following one:

for ii = 1 : 1000
  MM(:,ii) = betarnd(A,B); 
end

Thanks!

like image 598
Oliver Amundsen Avatar asked Mar 24 '26 17:03

Oliver Amundsen


1 Answers

You can repeat A and B (vectors of size 1x5000) to obtain matrices of size 1000x5000 in which all rows are equal, and use those matrices as inputs to betarnd. That way you get a result of size 1000x5000 in which column k contains 1000 random values with parameters A(k) and B(k).

The reason is that, according to the documentation (emphasis mine):

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.

So, use

MM = betarnd(repmat(A(:).',1000,1), repmat(B(:).',1000,1));
like image 78
Luis Mendo Avatar answered Mar 26 '26 09:03

Luis Mendo