# Tranpose a matrix

I am getting an error. I do not understand why tho.

const transpose = function(matrix) {
let temp = ; // temp is an empty array

for (let r = 0; r < matrix.length; r++) {
for (let c = 0; c < matrix[r].length; c++) {
console.log(“the value of r is:”, r, `element in \${r} is: \${matrix[r]}`);
console.log(“the value of c is:”, c, `element in \${c} is: \${matrix[r][c]}`);
if (r === c) {
temp.push([matrix[r][c]]); //[matric[r][c]] indicates that it is pushing an array
console.log("temp is: ", temp);
}
else {
console.log("else’s matrix is: ",matrix[r][c]);
temp[c].push(matrix[r][c]); //RIGHT HERE, I AM GETTING AN ERROR.
}
}
}
//return temp;
}

transpose([
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]
]);

Can someone explain to me, why am I getting an error? I mean, once I push to the nested matrix, it should have rows & columns.

Hi!

In the future remember to format posts because it’s very hard to read the code otherwise [How to] Format code in posts - #7.

As to your problem, what I notice is that your matrix is structure in row-column format (which is standard and good). Now, when you go to your else statement you write this line

``````temp[c].push(matrix[r][c]);
``````

which is a bit confusing, because you’re pushing a single cell value (indexed r,c) into a column “row” (which would take a vector of values). I’ll detail at the end of this note why this feels maybe one degree too complicated (but it’s viable, potentially).

It helps to share what the error is as it will help to deal with the surface technical problem at hand.

In terms of a strategy for solving the problem, I think it’s maybe helpful to do it in a straight-forward way first and see if you can optimize it later. Consider the following:

• you can immediately measure the dimensions of your input matrix
• which means you can iterate and make the appropriately sized row vectors (arrays)
• then, there is no need for conditional if/else statements, you can just deposit every element knowing it will land where it needs to
• or you can iterate your input matrix creating column vectors into arrays, and then just doing one pass through your rows and pushing the column vector into the row all at once (still no need for if/else)

You might know it, but in case you don’t Gilbert Strang has a beautiful set of Linear Algebra lectures for free on MIT opencourseware. I think working with this type of stuff by hand a lot makes it easier to troubleshoot when translating it to code (not 100% necessary but I found it helped for me).

1 Like
``````temp[c].push(matrix[r][c]);
``````

I did this because my intention was to push it into the column instead of into the row. Does this makes sense?

and then before this, I am pushing all the elements in r===c to the row matrix. So they form the nested structure.

I guess what I am really be asking, where does temp[c] reference to in the temp array.

The error was a type error saying cannot push undefined.

At the risk of sounding pedantic, pen and paper are one of the better tools for mapping this out (ideal form first, then tracing your code).

The methods I suggested will circumvent the undefined issue, which is most probably caused by the values being exhausted (because of boundary issues in the iterations).
If anything, I would reconsider the need for conditionals and work from there.