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 6833931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:02:47+00:00 2026-05-26T23:02:47+00:00

I have a 2-d array for which I want to detect all locally maximal

  • 0

I have a 2-d array for which I want to detect all locally maximal array indices. That is, given an index (i, j), its maximum gradient is the largest absolute change from any of its 8 neighboring values:

Index: (i, j)

Neighbors:
(i-1,j+1)  (i,j+1)  (i+1,j+1)
(i-1,j)    [index]    (i+1,j)
(i-1,j-1)  (i,j-1)  (i+1,j-1)

Neighbor angles:
315           0            45
270        [index]         90
225          180          135

MaxGradient(i,j) = Max(|Val(index) - Val(neighbor)|)

The index is said to be locally maximal if its MaxGradient is at least as large as any of its neighbors’ own MaxGradients.

The output of the algorithm should be a 2-d array of tuples, or a 3-d array, where for each index in the original array, the output array contains a value indicating if that index was locally maximal and, if so, the angle of the gradient.

My initial implementation simply passed over the array twice, once to calculate the max gradients (stored in a temporary array) and then once over the temp array to determine the locally maximal indices. Each time, I did this via for loops, looking at each index individually.

Is there some more efficient way to do this in numpy?

  • 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-05-26T23:02:48+00:00Added an answer on May 26, 2026 at 11:02 pm

    As Cyborg pointed out, there are only four differences which need to be computed to complete your calculation (note that there really should be a factor of 1/sqrt(2) for the diagonal and antidiagonal calculations if this really is a spatial gradient calculation on a uniform grid). If I have understood your question, the implementation with numpy could be something like this:

    A=np.random.random(100).reshape(10,10)
    
    # Padded copy of A
    B=np.empty((12,12))
    B[1:-1,1:-1]=A
    B[0,1:-1]=A[0,:]   
    B[-1,1:-1]=A[-1,:]
    B[1:-1,0]=A[:,0]
    B[1:-1,-1]=A[:,-1]
    B[0,0]=A[1,1]
    B[-1,-1]=A[-1,-1]
    B[-1,0]=A[-1,0]
    B[0,1]=A[0,1]
    
    # Compute 4 absolute differences
    D1=np.abs(B[1:,1:-1]-B[:-1,1:-1]) # first dimension
    D2=np.abs(B[1:-1,1:]-B[1:-1,:-1]) # second dimension
    D3=np.abs(B[1:,1:]-B[:-1,:-1]) # Diagonal
    D4=np.abs(B[1:,:-1]-B[:-1,1:]) # Antidiagonal
    
    # Compute maxima in each direction
    M1=np.maximum(D1[1:,:],D1[:-1,:])
    M2=np.maximum(D2[:,1:],D2[:,:-1])
    M3=np.maximum(D3[1:,1:],D3[:-1,:-1])
    M4=np.maximum(D4[1:,:-1],D4[:-1,1:])
    
    # Compute local maximum for each entry
    M=np.max(np.dstack([M1,M2,M3,M4]),axis=2)
    

    That will leave your with the maximum difference in each of the 4 directions of the input A in M. A similar idea can be used for labelling the locally maximal values, culminating in something like

    T=np.where((M==np.max(np.dstack([Ma,Mb,Mc,Md,Me,Mf,Mg,Mh]),axis=2)))
    

    which would give you an array contained the coordinates of locally maximal values in M

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of Paths which i want to read out with Template
I have an array which is a list of domains, I want to print
I have an array of file names which I want to download. The array
i have an char array b[20] which i want to write into a file
Consider I have an array of elements out of which I want to create
Suppose I have some pointer, which I want to reinterpret as static dimension array
I have an array which i need to sort its elements by occurrence then
I have a byte array which i want to copy/clone to avoid calling code
I have an array of images which I want to display as buttons in
I have an array which is filled asynchronous and contains 28 items. I want

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.