MATLAB: How do you create an array around a central value

arrays

I would like to write a function such as:
function [ array ] = generate_array( center_pt, resolution )
NB_POINTS = 5;
(…)
end
so that calling [ array ] = generate_array( 5, 2.5 )
array = [0 2.5 5 7.5 10]

Best Answer

How about this? It has three arguments, since I don’t know if you want the number of points to be an argument or decided by the function.
NrPts = 5;
CtrPt = 5;
Res = 2.5;
generate_array = @(CtrPt,NrPts,Res) linspace(CtrPt-Res*fix(NrPts/2), CtrPt+Res*fix(NrPts/2), NrPts);
It generates the array you specified, and seems to be robust for other argument values.