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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:59:04+00:00 2026-06-15T14:59:04+00:00

I have a 7*7 matrix containing 0s and 1s in which each (x,y) will

  • 0

I have a 7*7 matrix containing 0s and 1s in which each (x,y) will be checked for how many of its neighbors are a 1. I am a beginner to python, and will only be using basic programming procedures.

I have:

for x in range(rows):
        for y in range(cols):
            lives = 0
            lives = neighbors(matrix, rows, cols)

def neighbors(matrix, rows, cols):

            if matrix[x][y+1] == 1:
                lives += 1
            if matrix[x-1][y+1] == 1:
                lives += 1
            #All 8 positions are checked like this
    return lives

I am getting the ol indexing error. This seems like a very simple problem I just can’t seem to figure out how to fix it.

  • 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-15T14:59:05+00:00Added an answer on June 15, 2026 at 2:59 pm

    First of all, the index error occurs when you do y+1. Since you are going in the range of the amount of cols, this will end up being cols+1, which is out of range.
    What you can do is use a try-except block, or make sure it doesn’t get out of range through only looping to cols-1.

    Additionally your function definition is redundant, since you don’t use all your input parameters, and you access the x and y variables in the global scope.
    The easiest thing to do is probably just to remove the definition and the return-statement.

    This should work:

    for x in range(rows):
        for y in range(cols-1): #Loop until the second to last element.
            lives = 0
            if matrix[x][y+1] == 1:
                lives += 1
            if x == 0:  #You probably don't want to check x-1 = -1
                continue 
            if matrix[x-1][y+1] == 1:
                lives += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xts numeric matrix on which I need to apply many transformations.
Suppose I have a column matrix pols containing vectors of [theta, rho, z]. Which
I have a matrix A which I want to convert into a data.frame of
I have a matrix m of zeros and a data-frame df containing records I
So I have a file containing a matrix of integers. I need to read
I have a 3x3 matrix of 9 images built with Susy. I only wondered
I have to generate two random sets of matrices Each containing 3 digit numbers
I have written the code below to generate a matrix containing what is, to
I have a matrix ( X ) of doubles containing time series. Some of
I have a matrix M in MATLAB, containing m x n numbers. 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.