Question

I'm working with numerical methods and decided to begin learning with the Matlab environment, my question is, how can I add to the generated interval of my vector the last number of the interval, e.g.:

vector = [-2.4:2.4]

this will result in these numbers inside the vector:

 -2.4000   -1.4000   -0.4000    0.6000    1.6000

so, I wanna know what are my options so that I can do this:

 -2.4000   -1.4000   -0.4000    0.6000    1.6000    2.4000

I need the interval between the numbers to be 1 and I don't know the exact size of the vector, so I can't use linspace function. Before coming here to ask, I've already searched about it, but really didn't find something that could help me.

Was it helpful?

Solution

If the difference between the first element and the last element is not a multpiple of 1 you cannot have the interval between numbers be 1 for all. However, if your goal is to ensure that the last element is some particular number and are willing to compromise (it seems you do with 1.6 and 2.4), how about constructing v like this?

v1 = -2.4; v_last = 2.4;
v = v1 : v_last;
if v(end) ~= v_last
    v = [v, v_last];
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top