3

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!

4
  • Is there a specific need to use A and B as 1x5000 arrays? Commented Apr 10, 2015 at 20:37
  • @madbitloman I assume because there are 5000 different values Commented Apr 10, 2015 at 21:08
  • From your code I think you want a 1000x5000 matrix, not 5000x1000. Right? Commented Apr 10, 2015 at 21:13
  • hi there, thanks for the answers. @madbitloman, yes, I need the 5000 values because I need to sample that much pdfs. Luis, yes you're right. As I wrote it, it should be 1000x5000. Commented Apr 12, 2015 at 1:52

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

2 Comments

nice and clean. Thanks Luis!
@OliverAmundsen Great! I wasn't sure whether that was what you needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.