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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:57:35+00:00 2026-06-06T03:57:35+00:00

I have a code where I want to loop over all points in a

  • 0

I have a code where I want to loop over all points in a grid, and for each point check if a given condition holds for a sufficient number of neighbouring points. Additionally, I have periodic boundaries on the grid.

The problem is very similar to the Game of Life.

My current code looks something like this

do k=1,ksize; do j=1,jsize; do i=1,isize ! Loop over all points
  ncount = 0
  kkloop: do kk=k-1,k+1 ! Loop over neighbours
    ktmp = kk
    if(kk>ksize) ktmp = 1 ! Handle periodic boundary
    if(kk<1) ktmp = ksize
    do jj=j-1,j+1
      jtmp = jj
      if(jj>jsize) jtmp = 1
      if(jj<1) jtmp = jsize
      do ii=i-1,i+1
        if(ii == 0 .and. jj == 0 .and. kk == 0) cycle ! Skip self
        itmp = ii
        if(ii>isize) itmp = 1
        if(ii<1) itmp = isize

        if(grid(itmp,jtmp,ktmp)) ncount = ncount + 1 ! Check condition for neighbour
        if(ncount > threshold) then ! Enough neigbours with condition?
          do_stuff(i,j,k)
          break kkloop
        end if
      end do
    end do
  end do
end do; end do; end do

This is neither elegant, nor probably very efficient. Is there a better way to do this? This code will be repeated a lot, so I would like to make it as fast as possible.

  • 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-06T03:57:36+00:00Added an answer on June 6, 2026 at 3:57 am

    I’ll work this out in 2D, leave it to you to inflate to 3D.

    The first thing I’d do is pad the array with a halo of depth equal to the depth of the neighbourhood you are interested in. So, if your array is declared as, say

    real, dimension(100,100) :: my_array
    

    and you are interested in the 8 immediate neighbours of each cell,

    real, dimension(0:101,0:101) :: halo_array
    .
    .
    .
    halo_array(1:100,1:100) = my_array
    halo_array(0,:) = my_array(100,:)
    ! repeat for each border, mutatis mutandis
    

    This will save a lot of time checking for the boundary and will be worth doing whether or not you follow the next suggestion. You could do this ‘in place’ if you like, I mean just expand my_array rather than copy it.

    For an elegant solution you could write something like this

    forall (i=1:100,j=1:100)
       if (logical_function_of(my_array(i-1,j),my_array(i+1,j),my_array(i,j-1),my_array(i,j+1),...) then
          do_stuff(my_array(i,j))
       end if
    end forall
    

    Here, logical_function_of() returns true when the neighbourhood of my_array(i,j) satisfies your criteria. I got tired after listing the N,S,E,W neighbours and for production code I’d probably write this as a function of the indices anyway. In my experience forall is elegant (to some) but not as high-performing as nested loops.

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

Sidebar

Related Questions

I have the code below and I want to loop this every 5 seconds.
I have code that I want to compile on all unix systems, but if
I have this code that I want to make point-free; (\k t -> chr
Suppose you have 3 arrays you want to loop over, with lengths x, y,
I have code that I want to look like this: List<Type> Os; ... foreach
I have code that I want to be executed before every action is called.
I have the following code I want to run, but the problem is $this->type
I have some code that I want to put into an svn repository for
I have a code base I want to use for both an ASP.NET MVC
I have following code and i want to pass additional parameter in JSON.getJSONfromURL() method

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.