MatLab: array of numbers unequal distribution

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 4K views
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: 857
  • Like
Likes   Reactions: 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