Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Discussions
- :
- Range around maximum

Topic Options

- Start Article
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Apr 9, 2015 1:41 AM
(4512 views)

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

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**)**;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Range around maximum

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