How to choose a single element randomly from a vector

조회 수: 20 (최근 30일)
Yogesh 2024년 8월 3일
댓글: Yogesh 2024년 8월 4일
A=[2 3 4 5];
How do I choose any one variable from the vector A randomly each time.

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

채택된 답변

R 2024년 8월 3일
To choose a random element from the vector A each time in MATLAB, you can use the randi function to generate a random index and then use that index to access an element from A.
See below:
A = [2 3 4 5];
randomElement = A(randi(length(A)));
disp(randomElement);
5
This code will select and display a random element from the vector A each time it is run.
To know more about random number generation, refer to: Random Number Generation - MATLAB & Simulink (mathworks.com)
댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Yogesh 2024년 8월 4일
Hey thanks man...

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

추가 답변 (1개)

John D'Errico 2024년 8월 3일
편집: John D'Errico 2024년 8월 3일
I've upvoted the answer by @R. Please accept it. My answer here is only to express some ideas that are really best put in a comment to that answer, but comments often get lost.
When you have a problem you can't solve, one too big of a step to make for your current skills, break it into smaller pieces. Look for ways to approach a solution, in smaller chunks. Always remember to eat a programming elephant one byte at a time.
For example, consider the vector V.
V = [2 3 5 7 11 13 17 19];
Here, a list pf prime numbers. Suppose I want to generate a random prime from that list? First, how do you extract one element? What, for example is the 5th element from that vector?
V(5)
ans = 11
So we know how to index into a vector. Does this help us in any way? Well, possibly. Is there some way we can generate a random index? There are 8 elements in the vector V.
nV = numel(V)
nV = 8
Suppose we could generate a random integer, from the set 1 through 8? Could we then use that number as an index into V?
What tools are there in MATLAB to generate random numbers? The big three are rand, randn, and randi. Of those three, rand and randn generate numbers from continuous distributions, thus uniform and Gaussian. They are not directly useful here. (though with some effort, we could use rand.) But randi should seem useful.
help randi
RANDI Pseudorandom integers from a uniform discrete distribution. R = RANDI(IMAX,N) returns an N-by-N matrix containing pseudorandom integer values drawn from the discrete uniform distribution on 1:IMAX. RANDI(IMAX,M,N) or RANDI(IMAX,[M,N]) returns an M-by-N matrix. RANDI(IMAX,M,N,P,...) or RANDI(IMAX,[M,N,P,...]) returns an M-by-N-by-P-by-... array. RANDI(IMAX) returns a scalar. RANDI(IMAX,SIZE(A)) returns an array the same size as A. R = RANDI([IMIN,IMAX],...) returns an array containing integer values drawn from the discrete uniform distribution on IMIN:IMAX. Note: The size inputs M, N, P, ... should be nonnegative integers. Negative integers are treated as 0. R = RANDI(..., CLASSNAME) returns an array of integer values of class CLASSNAME. R = RANDI(..., 'like', Y) returns an array of integer values with the same data type and complexity (real or complex) as the numeric variable Y. The arrays returned by RANDI may contain repeated integer values. This is sometimes referred to as sampling with replacement. To get unique integer values, sometimes referred to as sampling without replacement, use RANDPERM. The sequence of numbers produced by RANDI is determined by the settings of the uniform random number generator that underlies RAND, RANDN, and RANDI. RANDI uses one uniform random value to create each integer random value. Control that shared random number generator using RNG. Examples: Example 1: Generate integer values from the uniform distribution on the set 1:10. r = randi(10,100,1); Example 2: Generate an integer array of integer values drawn uniformly from 1:10. r = randi(10,100,1,'uint32'); Example 3: Generate integer values drawn uniformly from -10:10. r = randi([-10 10],100,1); Example 4: Reset the random number generator used by RAND, RANDI, and RANDN to its default startup settings, so that RANDI produces the same random numbers as if you restarted MATLAB. rng('default'); randi(10,1,5) Example 5: Save the settings for the random number generator used by RAND, RANDI, and RANDN, generate 5 values from RANDI, restore the settings, and repeat those values. s = rng i1 = randi(10,1,5) rng(s); i2 = randi(10,1,5) % i2 contains exactly the same values as i1 Example 6: Reinitialize the random number generator used by RAND, RANDI, and RANDN with a seed based on the current time. RANDI will return different values each time you do this. NOTE: It is usually not necessary to do this more than once per MATLAB session. rng('shuffle'); randi(10,1,5) See also RAND, RANDN, RANDPERM, RNG, RANDSTREAM Documentation for randi doc randi Other uses of randi codistributed/randi distributed/randi codistributor1d/randi gpuArray/randi codistributor2dbc/randi RandStream/randi
Do you see that randi can generate random integers from the set 1:nV?
randi(nV)
ans = 3
randi(nV)
ans = 6
Does that help? I hope so. Use indexing to give you what you need.
ind = randi(nV)
ind = 4
V(ind)
ans = 7
The point is, to look for tools that might help you to get at least closer to a solution. Then think about how you might use them, sometimes in combination with oher tools you already know how to use, to then solve your problem.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by