MatLab: array of numbers unequal distribution

Click For Summary

Discussion Overview

The discussion revolves around generating an array of numbers in MatLab that are clustered around a specified point x1, with an exponential distribution of points that become denser near x1 and sparser towards the boundaries of the range (0 to 0.1). Participants explore methods to achieve this clustering and later discuss how to calculate a weighted mean of the generated points.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant describes their goal of creating a clustered array using tanh and linspace, noting challenges with variable limits.
  • Another participant proposes a function that generates clustered numbers around x1, providing a code snippet and an example plot for clarification.
  • A similar code snippet is repeated by a participant, indicating a possible oversight or emphasis on the solution.
  • There is a suggestion to use logspace as an alternative to linspace for generating values.
  • A participant inquires about calculating the weighted mean of the points if each point has an associated value, expressing concerns about the unequal spacing of points.
  • Another participant suggests using a dot product for calculating the mean if weights are assigned to the values associated with the points.
  • A further clarification is provided regarding the calculation of the weighted mean, emphasizing that weights can vary and that the standard mean formula applies when all weights are equal.

Areas of Agreement / Disagreement

Participants generally agree on the approach to generating clustered numbers and the concept of calculating a weighted mean, but there are differing views on the specifics of implementing the weighted mean calculation, particularly concerning the treatment of unequal spacing.

Contextual Notes

Some participants express uncertainty about the implications of unequal spacing on the mean calculation, and there is no consensus on the best method to account for this in practice.

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: 843
  • 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
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K