Here's an implementation of the linspace
function in MATLAB:
main.m260 chars8 lines
This function takes in the starting value x1
, the ending value x2
, and the number of points n
that should be evenly spaced between those two values. It then calculates the step size between each point ((x2 - x1)/(n-1)
) and generates a row vector of n
evenly spaced points starting at x1
and incrementing by step
until x2
is reached.
You can call this function just like you would the built-in linspace
function:
main.m26 chars2 lines
This would generate a row vector x
with 5 evenly spaced points between 0 and 1:
main.m24 chars2 lines
gistlibby LogSnag