0

I'm trying to change the Array Prototype and access columns as if they are rows therefor doing a custom matrix transposition. I want to make sure that the access of each column doesn't require re-allocating and creating an entirely new object just to access a particular column.

For example...

var mcol = new column([[0,1,2],[3,4,5],[6,7,8]]);
alert(mcol[1]);

What I'm looking for is to read the column as if it was a row... (doing a 90 degree transform on the matrix)

mcol[1] = [1,4,7];

Any suggestions?

3 Answers 3

2

You can use this constructor:

function column() {
    var arr = Array.prototype.slice.call(arguments);

    return arr;
}

Allowing you to do what you want.

var mcol = new column([0,1,2], [3,4,5], [6,7,8]);
alert(mcol[1]); // alerts [3, 4, 5]
Sign up to request clarification or add additional context in comments.

1 Comment

What I was looking to do is do a matrix transform without copying the entire array. ROW 0 -> COLUMN 0 and ROW 1 -> COLUMN 1 and so on...
1

I'd suggest you make a Matrix constructor with a column method:

function Matrix() {
  this._rows = Array.prototype.slice.call(arguments);
}
Matrix.prototype.column = function (i) {
  // See (1)
  return this._rows.map(function (row) {
    return row[i];
  });
};

var m = new Matrix([0,1,2],[3,4,5],[6,7,8]);
console.log(m.column(1)); // [1,4,7]

(1) https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map

3 Comments

why does javascript need to reserve the word prototype. shouldn't we be able to use Array.slice.call(arguments);
What that line is doing is using a function that all arrays have on the arguments object which is a special object that looks like an array but isn't one. Array.slice isn't defined, but [].slice is defined. So you borrow from the array and call() on arguments.
Array.prototype.slice.call(arguments) is pretty much standard practice in the JavaScript community. It's a way of getting a real array from an arguments object. Map won't work for that. concat() also works.
0

The solution I have is a work around and involves copying the array to a new array which I don't like. It does override the Array prototype.

    Array.prototype.rotate = function () {
      var rotated = [];
      var columns = 1; 
      for(var col = 0; col < columns; col++) {
        var myrow = [];
        for(var row = 0; row < this.length; row++){ // this.length is the longest column
          if(this[row].length > columns){
            columns = this[row].length;
          }
          if(this[row].length > col){
            myrow.push(this[row][col]);
          } else {
            myrow.push(null);
          }
        }
        rotated.push(myrow);
      }
      return rotated;
    }

var mcol = [[0,1,2], [3,4,5], [6,7,8]];
mcol = mcol.rotate();
alert(mcol[1]);

Alerts [1,4,7]

Does anyone know of a solution that doesn't require someone to copy the entire array to a new array?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.