Generating random 3D vectors

조회 수: 10 (최근 30일)
N/A
N/A 2016년 10월 28일
댓글: Charles Jing 2022년 3월 6일
Hi, I'm trying to create 100 random 3D vectors with entries [-1,1]. I'm new to matlab so it's very confusing to me at the moment.
I would very much appreciate your help.
Thank you.

답변 (3개)

John D'Errico
John D'Errico 2016년 10월 28일
편집: John D'Errico 2016년 10월 28일
rand(100,3)*2-1
Read the help. It shows how to do exactly this type of thing.
doc rand
I'm not sure why you are confused though. Rand generates random numbers between 0 and 1. Multiply by 2, and they are between 0 and 2. Subtract 1, and what do you have?
The above assume that you intended elements that lie in the interval [-1,1]. That is what that notation shows of course. If you really intended only the integer elements -1 and 1 as possibilities, then you should say so clearly. But that too is easy, and is a trivial extension of the above.
round(rand(100,3))*2-1
Then you want to generate 100 sets of 3 such numbers. A vector is just a list of three numbers. 100 of them is an array, so store them in a100x3 array.

James Tursa
James Tursa 2016년 10월 28일
편집: James Tursa 2016년 10월 28일
E.g., see this discussion:
If you mean a 3-element vector (i.e., a vector in 3-space), then just:
v = 2*round(rand(3,100))-1; % for values from the discrete integer set -1,+1
or
v = 2*rand(3,100)-1; % for floating point values between -1 to +1
If you mean 3-element vectors with endpoints uniformly distributed on a unit sphere, then you would need to do something else. E.g. using the randn function:
v = randn(3,100);
v = bsxfun(@rdivide,v,sqrt(sum(v.*v)));

Charles Jing
Charles Jing 2022년 3월 5일
I guess is Jason asking for this?
x=rand(100,100,100)*2-1;
  댓글 수: 2
John D'Errico
John D'Errico 2022년 3월 5일
Surely not so. That generates an array of size 100x100x100. They are not sets of random numbers in a 3-dimensional space, but a 3-dimensional array of numbers. Had you said
x = rand(100,3)*2-1;
or
x = rand(3,100)*2-1;
then your answer would have been correct. In these two cases, you can now view the rows (or columns as appropriate) as sets of random numbers in a 3-dimensional space, so R^3.
Remember, the request was to generate 100 random 3-d vectors.
Charles Jing
Charles Jing 2022년 3월 6일
Agree John, thanks!

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by