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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:23:09+00:00 2026-06-17T18:23:09+00:00

Given a matrix of M rows and N columns, and allocated as a byte

  • 0

Given a matrix of M rows and N columns, and allocated as a byte array of M*N elements (these elements are initially set to zero), I would modify this matrix in according to the following rule: the elements that are found in the neighborhood of a certain element must be set to a given value. In other words, given a matrix, I should set a region of the matrix: for this purpose I should access not contiguous portion of the array.

In order to perform the above operation, I have access to the following information:

  • the pointer to the element that is located in the center of the neighborhood (this pointer must not be changed during the above operation); the position (row and column) of this element is also provided;
  • the size L*L of the neighborhood (L is always an odd number).

The code that implements this operation should be executed as fast as possible in C++: for this reason I thought of using the above pointer to access different pieces of the array. Instead, the position (row and column) of the central element of the neighborhood could allow me to check whether the specified region exceeds the dimensions of the matrix (for example, the center of the region may be located on the edge of the matrix): in this case I should set only that part of the region that is located in the matrix.

int M = ... // number of matrix rows
int N = ... // number of matrix columns

char* centerPtr = ... // pointer to the center of the region
int i = ... // position of the central element
int j = ... // of the region to be modified

char* tempPtr = centerPtr - (N+1)*L/2;
for(int k=0; k < L; k++)
{
    memset(tempPtr,value,N);
    tempPtr += N;
}

How can I improve the code?
How to handle the fact that one region may exceeds the dimensions of a matrix?
How to make the code more efficient with respect to the execution time?

  • 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-17T18:23:11+00:00Added an answer on June 17, 2026 at 6:23 pm

    Your code is probably optimal for the general case where the region does not overlap the outside of the matrix. The main efficiency problem you can cause with this kind of code is to make the outer loop over columns instead of rows. This destroys cache and paging performance. You haven’t done that.

    Using pointers has little or no speed advantage with most modern compilers. Optimizers will come up with very good pointer code from normal array indices. In some cases I’ve seen array index code run substantially faster than hand-tweaked pointer code for the same thing. So don’t use pointer arithmetic if index arithmetic is clearer.

    There are 8 boundary cases: north, northwest, west, …, northeast. Each of these will need a custom version of your loop to touch the right elements. I’ll show the northwest case and let you work out the rest.

    The fastest possible way to handle the cases is a 3-level “if” tree:

    if (j < L/2) {  // northwest, west, or southwest
      if (i < L/2) {  
        // northwest
        char* tempPtr = centerPtr - (L/2 - i) * N - (L/2 - j);
        for(int k = 0; k < L; k++) {
          memset(tempPtr, value, L - j);
          tempPtr += N;
        }
      } else if (i >= M - L/2) { 
        // southwest
      } else { 
        // west
      }
    } else if (j >= N - L/2) { // symmetrical cases for east.
      if (i < L/2) {  
        // northeast
      } else if (i >= M - L/2) { 
        // southeast
      } else { 
        // east
      }
    } else { 
      if (i < L/2) {  
        // north
      } else if (i >= M - L/2) { 
        // south
      } else { 
        // no overlap
      }
    }
    

    It’s tedious to do it like this, but you’ll have no more than 3 comparisons per region.

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

Sidebar

Related Questions

Given the number of rows (or columns) , n, of a square matrix, I
Given a matrix with 25 rows and 80 columns but with the attributes its
this is a google interview question : Given a N*N Matrix. All rows are
I think Mathematica is biased towards rows not columns. Given a matrix, to insert
Given: square matrix, and list which represents the index of rows to be removed,
I have a set of data in a matrix which I would like to
I have a 2d matrix, i want to traverse all elements in this matrix
I have a very large matrix(10x55678) in numpy matrix format. the rows of this
for a given data frame I would like to multiply values of an array
If I have a matrix given as a list of rows [[1,2,3],[4,5,6]], 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.