BookmarkSubscribeRSS Feed
Choose Language Hide Translation Bar
Highlighted
Community Trekker

## Range around maximum

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!

Thank you for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Staff

## Re: Range around maximum

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())),

AddRows(100)

);

// 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 2
Staff

## Re: Range around maximum

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())),

AddRows(100)

);

// 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

## Re: Range around maximum

That's perfect, thank you very much Ian for your exhaustive answer!