이전 댓글 표시
Hello i want to know if exist a function that generate numbers in a range that i want to fix. for example i need to generate 22 numbers in this range: 0.5000 and 0.600
can you help me? thank you
답변 (2개)
Aldin
2012년 3월 20일
Here:
0.5:0.1/20:0.6
Beacuase: (0.6 - 0.5)/20
댓글 수: 5
Oleg Komarov
2012년 3월 20일
Actually it's:
a = 0.5:0.1/21:0.6;
Alternatively a high level function (which uses the engine showed by Aldin)
b = linspace(.5,.6,22);
Geoff
2012년 3월 20일
Using linspace to generate a series is preferable here because it has more transparent semantics. _ie_ "I want 22 values between A and B". It's also easier to change the range without introducing errors. What if you decided you needed to go between 0.5 and 0.7? How will you make sure you don't make a mistake with the colon operator?
Aldin
2012년 3월 20일
Hi Oleg, when yout write 0.1/21 the length of the vector is 23 not 22
Oleg Komarov
2012년 3월 20일
In my case is 22.
Geoff
2012년 3월 20일
Oleg is correct: length( 0.5:0.1/20:0.6 ) is 21. You really should use linspace if you want a linear sequence with a specified number of elements. That's what it's there for.
Geoff
2012년 3월 20일
You haven't really specified if you want a sequence or just a set of numbers. Picking up on your term 'generate', perhaps you meant this:
randarr = @(lo, hi, n) lo + (hi-lo) * rand(1,n);
data = randarr(0.5, 0.6, 22);
That creates a bunch of random numbers in your specified range. Note I've wrapped it in an anonymous function to give the same syntax as linspace.
댓글 수: 1
Geoff
2012년 3월 20일
Oops, just realised I specified that anonymous function incorrectly. Fixed.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!