MATLAB MatLab: array of numbers unequal distribution

AI Thread Summary
The discussion focuses on generating an array of numbers clustered around a specified point x1 within the range of 0 to 0.1 using MatLab. Users propose using functions like tanh and linspace to achieve an exponential clustering effect, with suggestions for alternative methods such as logspace. A sample function is provided to create the desired distribution, and a plot is generated to visualize the results. Additionally, there is a query about calculating the weighted mean of the generated points, with clarification that weights can be assigned to each point for accurate mean calculation. The conversation emphasizes the importance of considering unequal spacing when computing the mean.
AnneElizabeth
Messages
19
Reaction score
0
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..)
 
Physics news on Phys.org
Something like this perhaps:

Code:
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:
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.
 

Attachments

  • clustered_nums.png
    clustered_nums.png
    2.3 KB · Views: 818
  • Like
Likes AnneElizabeth
kreil said:
Something like this perhaps:

Code:
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:
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.
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.
 
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.
 
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.
xyvals.jpg
 

Similar threads

Back
Top