MATLAB: How to create a linspace with a condition

linspaceMATLAB

Hi
I want to create a linspace with a condition for the spacing, so that the spacing has a maximum allowed value. For example, if the spacing gets to high the function creates more points. Is this possible?

Best Answer

Hi,
if you know the maximum allowed spacing you can use:
function result = mySpacing(StartValue,EndValue,maxSpaceValue)
result = linspace(StartValue,EndValue,ceil((EndValue-StartValue)/maxSpaceValue+1));
end
This will ensure that the spacing is always smaller or equal to the given argument:
result1 = mySpacing(1,2,0.12)
result1 =
Columns 1 through 9
1.0000 1.1111 1.2222 1.3333 1.4444 1.5556 1.6667 1.7778 1.8889
Column 10
2.0000
result2 = mySpacing(0,2,0.15)
result2 =
Columns 1 through 9
0 0.1429 0.2857 0.4286 0.5714 0.7143 0.8571 1.0000 1.1429
Columns 10 through 15
1.2857 1.4286 1.5714 1.7143 1.8571 2.0000
result3 = mySpacing(0,2,0.5)
result3 =
0 0.5000 1.0000 1.5000 2.0000
Best regards
Stephan