Thursday, November 30, 2006

MATLAB: Creat equidistant N points between A and B

Suppose we have a range [2 11], and we want to divide it into 10 separate equidistant ranges. Now the question is that what are the separators. At the first glance, we usually have the following MATLAB code:

[2: (11-2)/10:11]

>> [2: (11-2)/10:11]

ans =

2.0000 2.9000 3.8000 4.7000 5.6000 6.5000 7.4000 8.3000 9.2000 10.1000 11.0000

However, there is a function LINSPACE(X1, X2, N) that can handle it:

linspace(2,11,11)

>> linspace(2, 11,11)

ans =

2.0000 2.9000 3.8000 4.7000 5.6000 6.5000 7.4000 8.3000 9.2000 10.1000 11.0000

In LINSPACE(X1, X2, N), X1 and X2 are defined by the range, and N is the quantitiy of points including X1 and X2 in this range. Because we divide the range into 10 separate equidistant ranges which means that there are 11 points in total. Thus here N is 10+1=11.

No comments:

Post a Comment