MATLAB: How do I divide a vector into intervals?

139 Views Asked by At

I am new to MATLAB and programming in general. I'm afraid I am a bit lost at the moment.

I have been trying to write a MATLAB function that does the following:

Takes inputs "upper" and "number" which are both positive integers. The program should take the interval [$0,$"upper"] and cut it into "number" subintervals which have integer endpoints and are the same length, except for the last interval (if the interval isn't cleanly divisible by number, then the last interval can contain less integer entries).

Then the program should make a vector $x$ with entries that are the beginnings/ends of those subintervals.


For example, if I have upper $= 11$ and number $= 3,$ then the program should take the vector

R = [0 1 2 3 4 5 6 7 8 9 10 11]

and generate

x = [0 4 8 11].

It should first create subinterval vectors

r1 = [0 1 2 3 4]

r2 = [4 5 6 7 8]

r3 = [8 9 10 11]

and then loop through $x$ and assign $0$ to the first entry and the endpoints of the r's to the other entries of $x$ but I am at a loss as to how to tell MATLAB to do this.

My other problem is that I don't know how to tell MATLAB how to figure out if the interval [$0,$ "upper"] is cleanly divisible by "number" and if not, how to then do to make the last subinterval shorter than the others.

I've sat at this for several days, but I'm not getting anywhere.

Any ideas or pointers in the right direction would be greatly appreciated.

Thank you.