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

  • Home
  • SEARCH
  • 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 6814729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:43:23+00:00 2026-05-26T20:43:23+00:00

Here is the scenario: // getMatrix() returns int[]. It is 1-d // I wish

  • 0

Here is the scenario:

// getMatrix() returns int[]. It is 1-d
// I wish it was 2d.
int[] mat = MyMatrix.getMatrix();

// get height and width of the matrix;
int h = MyMatrix.height;
int w = MyMatrix.width;

// calculate the center index of the matrix
int c = ... // need help here

// manipulate the center element of the matrix.
SomeClass.foo(mat[c]);

Example: Suppose I have a 5 x 5 matrix:

* * * * * // index 0 to 4
* * * * * // index 5 to 9
* * * * * // index 10 to 14.
* * * * * // index 15 to 19
* * * * * // index 20 to 24

If getMatrix() were to return int[][], the center coordinate of this matrix would be (2,2) 0-index based. But since getMatrix() returns int[], the center coordinate index c is 12.

However, if the height or width of the matrix is even, the center index can one of its 2 or 4 centers as shown in a 6 x 6 matrix:

* * * * * *
* * * * * *
* * @ @ * *
* * @ @ * *
* * * * * *
* * * * * *

–> The center is any of the @ above.

How would I calculate for the center index c of an m x n matrix?

  • 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-26T20:43:24+00:00Added an answer on May 26, 2026 at 8:43 pm

    The center of the matrix is the center of the array. This is because there will be an equal number of rows above and below the center row. And on the center row, there will be an equal number of cells to the left and right of the center cell.

    int c = mat.length / 2;
    

    or, if you want:

    int c = (width * height) / 2;
    

    This assumes that there is a single center of the matrix. That is, there is an odd number of rows and columns.

    If you want the median (mean of all centers), it will become more complicated:

    int x1 = (width - 1)/2;
    int x2 = width/2;
    int y1 = (height - 1)/2;
    int y2 = height/2;
    double median = (mat[width*y1 + x1] + mat[width*y1 + x2] +
                     mat[width*y2 + x1] + mat[width*y2 + x2])*0.25;
    

    If you only need one of the center cells, pick one of the four combinations of x1,x2,y1,y2. Simplest would be:

    int c = width * (height / 2) + (width / 2); // lower right center
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Class person{ int name; }; I have a scenario here in which i'm parsing
Tackling a strange scenario here. We use a proprietary workstation management application which uses
Here's my scenario - I have an SSIS job that depends on another prior
Here is the scenario: I'm writing an app that will watch for any changes
Here's the scenario: A C# Windows Application project stored in SVN is used to
Here is the scenario that I have. I have a cvs repository in one
Here's the scenario-- you've got a JSON object on the browser, and let's say
Here's the scenario: You have a Windows server that users remotely connect to via
Here's the scenario: I have a textbox and a button on a web page.
Here's the scenario. I'm debugging my own app (C/C++) which is using some library

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.