## Range around maximum

Community Trekker

Joined:

Apr 8, 2015

Sorry in advance for this newbie question:

I have simply one x and one y columns. With a script I am trying to get a table with:

• the maximum y (no problem with this),
• the average in a defined range around the maximum,
• the x value corresponding to the maximum y.

Probably it is a very easy script, but I have not be able to progress on it!

1 ACCEPTED SOLUTION

Accepted Solutions

Staff

Joined:

Jun 23, 2011

Solution

Something like this would do. But note that 'LocMax()' returns the position of the first maximum, and if there are multiple maxima you will have to do more work. Also, if the window is truncated you may wish to adjust the calculation of the mean, depending on what you need.

NamesDefaultToHere(1);

// Make some data

dt = NewTable("X and Y",

NewColumn("X", Numeric, Continuous, Formula(RandomNormal())),

NewColumn("Y", Numeric, Continuous, Formula(RandomNormal())),

);

// Get the data into vectors

xVals = Column(dt, "x") << getAsMatrix;

yVals = Column(dt, "y") << getAsMatrix;

// Find the position of the largest y value

yMaxPos = LocMax(yVals);

// Get the corresponding x and y values at this position

yMax = yVals[yMaxPos];

xMax = xVals[yMaxPos];

// Define window size (consisting of an odd number of values)

winSize = 9;

// Calculate the positions of the window's edges

left = yMaxPos - (winSize - 1)/2;

right = yMaxPos + (winSize - 1)/2;

// Make sure the edges are in range

if(left < 1, left = 1);

if(right > NRow(yVals), right = NRow(yVals));

// Calculate the average y value in the window

yMean = Mean(yVals[left::right]);

// Print to Log Window

ClearLog();

Print(yMaxPos, yMax, xMax, winSize, left, right, yMean);

2 REPLIES

Staff

Joined:

Jun 23, 2011

Solution

Something like this would do. But note that 'LocMax()' returns the position of the first maximum, and if there are multiple maxima you will have to do more work. Also, if the window is truncated you may wish to adjust the calculation of the mean, depending on what you need.

NamesDefaultToHere(1);

// Make some data

dt = NewTable("X and Y",

NewColumn("X", Numeric, Continuous, Formula(RandomNormal())),

NewColumn("Y", Numeric, Continuous, Formula(RandomNormal())),

);

// Get the data into vectors

xVals = Column(dt, "x") << getAsMatrix;

yVals = Column(dt, "y") << getAsMatrix;

// Find the position of the largest y value

yMaxPos = LocMax(yVals);

// Get the corresponding x and y values at this position

yMax = yVals[yMaxPos];

xMax = xVals[yMaxPos];

// Define window size (consisting of an odd number of values)

winSize = 9;

// Calculate the positions of the window's edges

left = yMaxPos - (winSize - 1)/2;

right = yMaxPos + (winSize - 1)/2;

// Make sure the edges are in range

if(left < 1, left = 1);

if(right > NRow(yVals), right = NRow(yVals));

// Calculate the average y value in the window

yMean = Mean(yVals[left::right]);

// Print to Log Window

ClearLog();

Print(yMaxPos, yMax, xMax, winSize, left, right, yMean);

Community Trekker

Joined:

Apr 8, 2015