How to calculate the size of a vector of this form?

124 views Asked by At

In Matlab, it's easy to define a vector this way:

x = a:b:c, where a,b,c are real numbers, a < c and b <= c - a.

My problem is that I'm having troubles trying to define a formula to calculate the number of elements in x.

I know that the problem is solved using the size command, but I need a formula because I'm doing a version of a Matlab program (which uses vectors this way), in another language.

Thanks in advance for any help you can provide.

Best regards,

Víctor

1

There are 1 answers

1
Daniel On BEST ANSWER

On a mathematical level you could argue that all of these expressions return the same:

size(a:b:c)
size(a/b:c/b)
size(0:c/b-a/b)

Now you end up with integers from 0 to that term, which is:

floor((c-a)/b+1)

There is one problem: Floating point precision. The colon operator does repeated summing, don't know any possibility to predict reproduce that.