Hi all,
Is it possible to use randn with the min and max values being matrices?
For Example:
A = [1x28] %The numbers vary within this matrix
B = [1x28] %The numbers vary within this matrix
r = A + (B-A).*randn(500,28);
I'm trying to understand if this is mathematically possible, considering there are multiple min and max values. Will Matlab allocate each column a separate min and max value (this is what I am hoping it will do)? Or should this problem simply be tackled using a different approach?
What I am looking for is to create a random [500x28] matrix with varying min and max values that are specified in matrices A and B. I have used randn before for integer values and it has worked beautifully. I just don't know if it is mathematically correct to use it in this context. I would love some thoughts and information on the best approach to take.
Thanks in advance.

 채택된 답변

Walter Roberson
Walter Roberson 2016년 11월 28일

1 개 추천

You wrote
r = A + (B-A).*randn(500,28);
but the form you are using does not apply to randn(). randn() has infinite tails.
If you switch to
r = A + (B-A).*rand(500,28);
then that works fine in R2016b or later. In R2016a or earlier you would need
r = bsxfun(@plus, A, bsxfun(@times, B-A, rand(500,28)) );

댓글 수: 3

Zena Assaad
Zena Assaad 2016년 11월 28일
Thank you very much. I am using R2015a so I went with the second option and it worked very well. Out of curiosity, is Matlab allocating separate min max for each column?
Walter Roberson
Walter Roberson 2016년 11월 28일
With the bsxfun, it would copy the per-column (A) and ((B-A)) to all of the rows
Zena Assaad
Zena Assaad 2016년 11월 28일
OK that makes sense. Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 11월 28일

0 개 추천

You could do this:
A = randi(99, 1,28) % The numbers vary within this matrix
B = randi(99, 1,28) % The numbers vary within this matrix
r = zeros(500, 28);
for col = 1 : size(A, 2)
r(:, col) = A(col) + (B(col) - A(col)) .* rand(500, 1);
end
You might want to make sure B is more than A if that's what you expect.

댓글 수: 3

Zena Assaad
Zena Assaad 2016년 11월 28일
Thank you for this. I tried this option and Walters option and both worked well. I went with Walters option because it was only one line.
Image Analyst
Image Analyst 2016년 11월 28일
OK, though it can be a bit cryptic for beginners. That's why I went with something I thought would be easier for you to understand. Though if you can remember how bsxfun works, it can be handy.
Zena Assaad
Zena Assaad 2016년 11월 28일
It was actually helpful to see it laid out line by line. I did a bit of reading on bsxfun before using it and between the two answers it made sense :)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2016년 11월 28일

댓글:

2016년 11월 28일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by