Creating an array of given size and increment

This seems like a stupid question from someone with 8 years Matlab experience, but either I'm having a brain fade moment or there is no neat shortcut syntax to this.
I simply want to create an array with a known start, increment and number of elements.
Obviously if I have start, increment, end I can use the colon operator and if I have start, number of elements and end I can use linspace.
But is there really nothing neater than having to do the maths to work out the end point then use one of the above options? I'd rather just let the max be whatever it happens to be.
Calculating an explicit max in order to create a matrix with an implicit increment (using linspace) that should match exactly the increment I used to calculate the max seems very un-Matlab-like.

2 Comments

%0 to 200 in 10 increment
countInTen = linspace(0,20,21)*10 ;
A simpler way to count from 0 to 200 in increments of 10 is:
countByTens = 0:10:200;
In the original question, the user knew the starting point (0) and the increment (10) but couldn't use the colon operator because they knew how many fence rails they wanted to use (20 rails) or how many posts they wanted (21) rather than where they wanted their fence to end (200.)

Sign in to comment.

 Accepted Answer

How about the following?
xStart = 5;
dx = 0.001;
N = 2000;
x = xStart + (0:N-1)*dx;
Is that what you are looking for?

5 Comments

Thank you! That is indeed exactly what I am looking for. These seemingly trivial problems are the ones that niggle at me and drive me mad if I can't solve them.
Glad to have helped. Sometimes it's a matter of looking at a problem from a different perspective, which often requires a fresh pair of eyes.
This is brilliant and almost exactly what I want to do, except that I'd like the values in x to roll-over at 1024 (for instance). I've played with this a fair bit, but it's difficult when I can't quite understand how it works :0) Could someone help me out please? Regards Andrew
What do you mean by 'roll over'? something like this?
xStart = 5;
dx = 1.5;
N = 2000;
x = mod( xStart + (0:N-1)*dx, 1024 );
Yes, I was using mod, but obviously in the wrong places, thanks for your help. I think I understand a little more how it's working now aswell.
Andrew

Sign in to comment.

Products

Asked:

on 19 Aug 2014

Commented:

on 27 Jun 2019

Community Treasure Hunt

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

Start Hunting!