Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:11:06+00:00 2026-06-05T20:11:06+00:00

I have an R matrix with nonnegative numeric values. The matrix is effectively a

  • 0

I have an R matrix with nonnegative numeric values. The matrix is effectively a 2D height map, and I want to find all the local maxima in this matrix. For “flat” peaks, where neighboring elements are equal to each other (and they are collectively a local maximum), I don’t care what happens as long as I get at least one coordinate within each “flat” region.

Are there any functions to do this efficiently? Obviously I could write the loop manually to go through and test every element individually, but doing that in R would be quite slow. I need to do this for around a million matrices, with an average of about 884 elements per matrix.

Ideally there would be a function that takes the matrix as input and returns a 2-column matrix with column 1 being row coordinates, column 2 being the column coordinates, and one row for each local maximum in the matrix.

Local maxima on the edges of the matrix are allowed. Areas outside of the matrix can be treated as having zero height.

Reproducible example matrix to use:

set.seed(5)
msize <- 20 # Change this to whatever you like
x <- matrix(data=abs(rnorm(msize*2)), nrow=msize, ncol=msize)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:11:08+00:00Added an answer on June 5, 2026 at 8:11 pm

    The focal() function in the raster package is designed for calculations like this. The following code returns coordinates of all local maxima, including those on edges and those that are parts of “plateaus “.

    library(raster)
    
    ## Construct an example matrix
    set.seed(444)
    msize <- 10
    x <- matrix(sample(seq_len(msize), msize^2, replace=TRUE), ncol=msize)
    
    ## Convert it to a raster object
    r <- raster(x)
    extent(r) <- extent(c(0, msize, 0, msize) + 0.5)
    
    ## Find the maximum value within the 9-cell neighborhood of each cell
    f <- function(X) max(X, na.rm=TRUE)
    ww <- matrix(1, nrow=3, ncol=3) ## Weight matrix for cells in moving window
    localmax <- focal(r, fun=f, w=ww, pad=TRUE, padValue=NA)
    
    ## Does each cell have the maximum value in its neighborhood?
    r2 <- r==localmax
    
    ## Get x-y coordinates of those cells that are local maxima
    maxXY <- xyFromCell(r2, Which(r2==1, cells=TRUE))
    head(maxXY)
    #       x  y
    # [1,]  8 10
    # [2,] 10 10
    # [3,]  3  9
    # [4,]  4  9
    # [5,]  1  8
    # [6,]  6  8
    
    # Visually inspect the data and the calculated local maxima
    plot(r)   ## Plot of heights
    windows() ## Open a second plotting device
    plot(r2)  ## Plot showing local maxima
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a matrix M(x,y). I want to apply a threshold in all values
I have a matrix A which I want to convert into a data.frame of
I have a matrix of objects that contains data in this form: name A,2,name
I have a matrix X(1e4,20) which takes on values 0:4 . I'm interested in
I have a matrix (5x5) with values in them for example: Matrix (1,1) Value:
I have a matrix that stores x, y and z values as so: {x1,
I have a matrix which is 36 x 2, but I want to seperate
I have a matrix called matrix that looks like this: charge convexhull_xCoor convexhull_yCoor id
I have a matrix in MATLAB from which I want to sample every other
I have this matrix A, representing similarities of pixel intensities of an image. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.