Equally increasing decimal numbers in an array

30 ビュー (過去 30 日間)
Olivia Milton-thompson
Olivia Milton-thompson 2015 年 11 月 15 日
コメント済み: Star Strider 2015 年 11 月 15 日
Hello! This might be super simple but I keep seem to find an elegant way of doing this.
I simply want an array to list numbers consecutively between 0.1 and 1.0 going up in intervals of 0.1. I can't write it as 0.1:1 because it won't go up in increments of 0.1.
Does anyone know a simple way of doing this?
Thank you!

採用された回答

Star Strider
Star Strider 2015 年 11 月 15 日
Yes. You can supply a ‘step’ to the colon operator to produce whatever step values you want in your vector:
v = 0.1:0.1:1;
  2 件のコメント
Olivia Milton-thompson
Olivia Milton-thompson 2015 年 11 月 15 日
Brilliant, thank you! I thought it was something like that but I couldn't get it to work! I assume the middle 0.1 is the increment?
Star Strider
Star Strider 2015 年 11 月 15 日
Thank you! My pleasure!
Correct. The middle 0.1 here is the step. (If you don’t specificallly state the step, the default step is 1.)

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 11 月 15 日
You have to give a starting value, a step, and an ending value - you gave only two of those, which it takes as starting value and ending value, so use all three startingValue:stepValue:endingValue, or 0.1 : 0.1 : 1 (like Star said). Alternatively, you should know about this useful little utility called linspace(). It's similar except that you specify how many steps you want and it computes the step size for you. let's say you wanted 10 steps between 0.1 and 1.0. Then you could do
v = linspace(0.1, 1.0, 10); % linspace(start, stop, numSteps)
Both methods have their uses and you'll probably use both in the future.
  1 件のコメント
Star Strider
Star Strider 2015 年 11 月 15 日
The key difference is that the colon operator uses a fixed step, so the vector length will vary. The linspace function specifies a fixed length, and adjusts vector step size to fit the length.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by