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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:51:31+00:00 2026-06-12T21:51:31+00:00

In Javascript, I have a matrix with a variable number of rows and columns,

  • 0

In Javascript, I have a matrix with a variable number of rows and columns, which I wish to store in a multi-dimensional array.

The problem is that I need extra 3 columns and 3 extra rows with negative indexes in the matrix too. So the result for a 10×10 matrix should be a 13×13 array with indexes from -3 to 9.

I define the array with:

  var numberofcolumns = 10;
  var numberofrows = 10;
  var matrix = [];
  for (var x = -3; x < numberofcolumns; x++) {
    matrix[x] = [];
  }

Is this the right way to do this? Or is there a better way?

  • 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-12T21:51:32+00:00Added an answer on June 12, 2026 at 9:51 pm

    You could define the matrix as an object instead. You would lose some array functionality but you could still access matrix[-3] for example.

      var numberofcolumns = 10;
      var numberofrows = 10;
      var matrix = {};
      for (var x = -3; x < numberofcolumns; x++) {
        matrix[x] = [];
      }
      for (x in matrix) {
        console.log(matrix[x]);
      }
    

    Or you could define your own class starting as an object or array and work from there. Here’s something to get you started:

    function Matrix() { };
    
    Matrix.prototype.LBound = function()
    {
        var n;
        for (i in this) {
            if (!isNaN(i) && (isNaN(n) || n > i))
                n = parseInt(i);
        }
        return n;
    };
    
    Matrix.prototype.UBound = function()
    {
        var n;
        for (i in this) {
            if (!isNaN(i) && (isNaN(n) || n < i))
                n = parseInt(i);
        }
        return n;
    };
    
    Matrix.prototype.length = function()
    {
        var length = this.UBound() - this.LBound();
        return isNaN(length) ? 0 : length+1;
    };
    
    Matrix.prototype.forEach = function(callback, indexes)
    {
        if (!indexes) var indexes = [];
        for (var i = this.LBound(); i <= this.UBound() ; i++)
        {
            indexes[Math.max(indexes.length-1, 0)] = i;
            callback(this[i], indexes);
            if (this[i] instanceof Matrix)
            {
                var subIndexes = indexes.slice();
                subIndexes.push("");
                this[i].forEach(callback, subIndexes);
            }
        }
    };
    
    Matrix.prototype.val = function(newVal)
    {
        if (newVal) 
        {
            this.value = newVal;
            return this;
        }
        else
        {
            return this.value;
        }
    };
    

    Then you’d create your matrix as such

    var numberofcolumns = 10;
    var numberofrows = 10;
    
    var matrix = new Matrix();
    
    for (var i = -3; i < numberofcolumns; i++) {
        matrix[i] = new Matrix();
            for (var j = -4; j < numberofrows; j++) {
                matrix[i][j] = new Matrix();
                matrix[i][j].val("test " + i + " " + j);
            }
    }
    

    And you can run some cool functions on it

    console.log("Upper bound: " + matrix.LBound());
    console.log("Lower bound: " + matrix.UBound());
    console.log("Length: " + matrix.length());
    
    matrix.forEach(function(item, index) 
        {
            if (item.val()) 
                console.log("Item " + index + " has the value \"" + item.val() + "\""); 
            else
                console.log("Item " + index + " contains " + item.length() + " items"); 
        });
    

    DEMO: http://jsfiddle.net/uTVUP/

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

Sidebar

Related Questions

i have written a snake program using javascript.. the problem is that the snake
'i have JavaScript associative array name 'options' <script> options[dynamickey] = Number; </script> and i
Does jquery/javascript have any event model that you can attach to your objects? Basically
var object = {foo: 'bar'}; Does JavaScript have a way to determine that object.foo
Javascript I have code that will hide various sections in a MS CRM form
I have javascript code some thing like this -- var count=3; var pl=new Array(count);
I have Javascript that opens another window and registers a click handler for all
I have JavaScript that is doing activity periodically. When the user is not looking
I have JavaScript code which copies the value of input file and paste it
Does JavaScript have a convenient way to test if a variable matches one of

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.