필터 지우기
필터 지우기

asking about command in matlab

조회 수: 3 (최근 30일)
STAR
STAR 2022년 12월 28일
답변: John D'Errico 2022년 12월 28일
what this command mean in details
Energy = 0.17+rand(1,1)*(0.19-0.17)
thanks

답변 (2개)

the cyclist
the cyclist 2022년 12월 28일
Are you willing to spend some time learning MATLAB? You will fully understand that line of code if you
  • Watch the free, online MATLAB Onramp tutorial (maybe even just the first 30 minutes will be enough)
  • Read even a little of the documentation for the rand function

John D'Errico
John D'Errico 2022년 12월 28일
As much as I completely agree with @the cyclist, that you need to start to learn MATLAB, you also need to learn to take apart a line of code. That in itself is a skill you need to learn. And it applies to ANY programming language. You would start at the most basic element of the code, and then work out, based on the order of operations in the language you are using.
And in fact, if you just read the docs for rand, you will find that exact form of the expression discussed. (Not a command though.)
So first, start in the middle. What does rand(1,1) do?
help rand
RAND Uniformly distributed pseudorandom numbers. R = RAND(N) returns an N-by-N matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval(0,1). RAND(M,N) or RAND([M,N]) returns an M-by-N matrix. RAND(M,N,P,...) or RAND([M,N,P,...]) returns an M-by-N-by-P-by-... array. RAND returns a scalar. RAND(SIZE(A)) returns an array the same size as A. Note: The size inputs M, N, P, ... should be nonnegative integers. Negative integers are treated as 0. R = RAND(..., CLASSNAME) returns an array of uniform values of the specified class. CLASSNAME can be 'double' or 'single'. R = RAND(..., 'like', Y) is an array of uniform values with the same data type and complexity (real or complex) as the numeric variable Y. The sequence of numbers produced by RAND is determined by the settings of the uniform random number generator that underlies RAND, RANDI, and RANDN. Control that shared random number generator using RNG. Examples: Example 1: Generate values from the uniform distribution on the interval (a, b). r = a + (b-a).*rand(100,1); Example 2: Use the RANDI function, instead of RAND, to generate integer values from the uniform distribution on the set 1:100. r = randi(100,1,5); Example 3: Reset the random number generator used by RAND, RANDI, and RANDN to its default startup settings, so that RAND produces the same random numbers as if you restarted MATLAB. rng('default') rand(1,5) Example 4: Save the settings for the random number generator used by RAND, RANDI, and RANDN, generate 5 values from RAND, restore the settings, and repeat those values. s = rng u1 = rand(1,5) rng(s); u2 = rand(1,5) % contains exactly the same values as u1 Example 5: Reinitialize the random number generator used by RAND, RANDI, and RANDN with a seed based on the current time. RAND 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'); rand(1,5) See Replace Discouraged Syntaxes of rand and randn to use RNG to replace RAND with the 'seed', 'state', or 'twister' inputs. See also RANDI, RANDN, RNG, RANDSTREAM, RANDSTREAM/RAND, SPRAND, SPRANDN, RANDPERM. Documentation for rand doc rand Other uses of rand codistributed/rand distributed/rand qrandstream/rand codistributor1d/rand gpuArray/rand RandStream/rand codistributor2dbc/rand matlab/rand
READ THE DOCUMENTATION WHEN YOU DON'T UNDERSTAND SOEMTHING.
And then TRY IT OUT! For example:
rand(1,1)
ans = 0.6804
Along the way, you would learn about what rand returns, a stream of uniformly distributed random numbers. How many such numbers does the call rand(1,1) produce? (Exactly 1 of them.)
But where do they live? What does a uniformly distributed random number mean? What interval does the distribution lie in? If you don't know, then go back up and read the help again. Hint: It talks about the open interval (0,1).
Now, what happens when you mutlply such a number that originally lives in the interval (0,1), by some constant? Now where does that product live? In this case, you multiplied by the constant (0.19 - 0.17), which is just 0.02. It would seem logical that the product lies in the still open interval (0,0.02).
Similarly, now add some constant to that number. Where does it live?
As I said, you take apart the expression that you don't understand, startgin at the inside, then working out, going by the order of operations. So we would have this sequence of operations we need to understand.
rand(1,1)
rand(1,1)*(0.19 - 0.17)
0.17 + rand(1,1)*(0.19 - 0.17)
Each of them changes the previous result in a specific way. But we should be able to understand what the final result does, from each of those steps. Finally, perhaps a histogram might help.
X = 0.17 + rand(1,1000000)*(0.19 - 0.17);
histogram(X,'norm','pdf')
Of course now you might ask what this last line of code does as written. And again, you learn that by simply reading the help.

카테고리

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