Filter löschen
Filter löschen

How to generate vector with non-linear spacing?

43 Ansichten (letzte 30 Tage)
Ryan
Ryan am 14 Aug. 2024 um 12:01
Kommentiert: Ryan am 14 Aug. 2024 um 12:59
Hello,
I am attempting to generate an array with non-linear spacing between the elements but struggling to figure out a way to impliment this. I need an array x of points spaced from a real, negative value a to a real, positive value b. Ideally the spacing should follow a Gaussian distribution such that many points are clustered about x=0 in the vicinity of some finite width d. Conversely the spacing should be large at the boundaries. More generally it doesn't have to be Gaussian but some guidance on a scheme where I can specify a length d<<|b-a| across which many points should be distributed would be really appreciated.
I tried to work out an algorithm of sorts from pen and paper but it didn't really work and resulted in something that did not appear to be immediately solveable.
Thanks

Akzeptierte Antwort

John D'Errico
John D'Errico am 14 Aug. 2024 um 12:50
Bearbeitet: John D'Errico am 14 Aug. 2024 um 12:52
It sounds like you want a deterministic, non-random set of points. You can use the inverse gaiussian CDF to do what you want.
a = -2.5;
b = 3;
N = 40;
x = norminv(linspace(normcdf(a),normcdf(b),N));
xline(x,'r')
So most dense at zero, and a density that drops off away from there. I chose limits that are not centered around zero, but had they been centered, the set would be perfectly symmetric.
Of course, you can adjust the speed of fall off, by use of a different variance on the implicit Gaussian.
  1 Kommentar
Ryan
Ryan am 14 Aug. 2024 um 12:59
Yes that's looking exactly like what I need, thanks so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Matt J
Matt J am 14 Aug. 2024 um 12:27
Bearbeitet: Matt J am 14 Aug. 2024 um 12:35
a=-1;
b=+1;
d=1/(b-a)^2;
y=d*randn(1,500);
x=sort(y+(a+b)/2);
stem(x,x.^0); xlim([a,b])
  2 Kommentare
Ryan
Ryan am 14 Aug. 2024 um 12:29
Thanks for the suggestion, this is a possible option. Any way to get the spacing more regular?
Ryan
Ryan am 14 Aug. 2024 um 12:49
Ah I see what I meant by Gaussian is not in the sense of a random distribution but of a spacing that followed a pattern akin to a contiunous Gaussian function. I had envisaged an algorithm that produces regular values
Where Δis some constant. I don't think this algorithm "works" as such but I think it illustrates what I'm after. For what I am working on a random distribution may also work.

Melden Sie sich an, um zu kommentieren.


Aquatris
Aquatris am 14 Aug. 2024 um 12:40
The answer from another question here:
A = 1;
B = 5;
std = .9; % need to play with this to get desired shape within desired range,
% if too high, it does not become a normal distribution
% if too low, it does not cover the whole A-B range
mean = 3;
step1Result = mean + std * randn(1e3,1); % create random data with mean and std
step2Result = min(max(step1Result,A),B); % make sure they end up in A-B range
hist(step2Result)
ylabel('# of occurence')
xlabel('Data Value')

Kategorien

Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by