How to choose a single element randomly from a vector

39 次查看(过去 30 天)
A=[2 3 4 5];
How do I choose any one variable from the vector A randomly each time.

采纳的回答

R
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 个)

John D'Errico
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 中查找有关 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