# MatLab: array of numbers unequal distribution

Tags:
1. Apr 1, 2015

### AnneElizabeth

I want to create an array of numbers between 0 and 0.1 where the points are clustered around an arbitrary point x1 (0 < x1 < 0.1). I want the points to get exponentially closer together near x1 from either side and and get further apart towards the outer limits. I am using MatLab and was trying to use tanh and linspace.
tanh(linspace(0,6,50)) gives a suitable shape for 0 to x1 and -tanh(linspace(1,6,50)) for x1 to x2.
However I can't make it work for my limits, taking into account that x1 varies.

(I will later need to take a weighted mean of these points but that's another problem..)

2. Apr 2, 2015

### kreil

Something like this perhaps:

Code (Text):

function y = clusteredNumbers(N,x1)
% Generates 2*N numbers from 0 to 0.1, with N numbers on each side of x1.
% The numbers cluster about the point x1.
x0 = linspace(0,6,N);
z = x1*tanh(x0);
z1 = 0.1 - fliplr((0.1-x1)*tanh(x0));
y = [z'; z1'];
end

For x1 = 0.03 and N = 50, this produces the attached plot.

Code (Text):

y = clusteredNumbers(50, 0.03);
plot(y,ones(100,1),'.')

EDIT: An alternative to using linspace + tanh is to use logspace, which is similar to linspace but produces logarithmically spaced values.

File size:
3.3 KB
Views:
246
3. Apr 2, 2015

### AnneElizabeth

Thank you! That's exactly what I wanted!
If each of those points have a different value, any chance you know how to calculate the weighted mean of the whole line? Can't figure it out with the spacing between the points.

4. Apr 2, 2015

### kreil

Not sure I follow... Are these random numbers the weights you want to use for the associated values? If so you just dot product them together and divide by the number of elements.

5. Apr 3, 2015

### AnneElizabeth

I mean if I assign a y-value to each of those points, how would I calculate the mean of all the points, as if I just used the simple formula for mean of: (sum of the y-values)/(number of points) it wouldn't take into account that the points are unequally spaced.

6. Apr 3, 2015