Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MatLab: array of numbers unequal distribution

  1. Apr 1, 2015 #1
    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. jcsd
  3. Apr 2, 2015 #2

    kreil

    User Avatar
    Gold Member

    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.
     

    Attached Files:

  4. Apr 2, 2015 #3
    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.
     
  5. Apr 2, 2015 #4

    kreil

    User Avatar
    Gold Member

    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.
     
  6. Apr 3, 2015 #5
    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
     
  7. Apr 3, 2015 #6

    kreil

    User Avatar
    Gold Member

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MatLab: array of numbers unequal distribution
  1. Matlab and arrays (Replies: 0)

  2. Arrays in MATLAB (Replies: 3)

Loading...