Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

JavaScript - transpose matrix

0 contributions
3 points

In this short article we would like to show how to transpose matrix in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

function transposeMatrix(matrix) {
	var result = new Array(matrix.length);
	for (var i = 0; i < matrix.length; ++i) {
		result[i] = new Array(matrix[0].length);
	}
	for (var i = 0; i < matrix.length; ++i) {
		for (var j = 0; j < matrix[0].length; ++j) {
			result[j][i] = matrix[i][j];
		}
	}
	return result;
}

// Helper methods:

function printMatrix(matrix) {
	var text = '';
	for (var i = 0; i < matrix.length; ++i) {
		var row = matrix[i];
		for (var j = 0; j < row.length; ++j) {
			text += row[j] + ' ';
		}
		text += '\n';
	}
	console.log(text);
}

// Usage example:

var matrix = [
	[1, 2, 3],
	[4, 5, 6],
	[7, 8, 9]
];
var transposedMatrix = transposeMatrix(matrix);

printMatrix(transposedMatrix); // 1 4 7 
                               // 2 5 8 
                               // 3 6 9 

Note: above example do not check sizes of arrays.

Matrix transposition with sizes checking example

Solution presented in this section checks each row size and inform if transposition can not be done.

// ONLINE-RUNNER:browser;

function createMatrix(width, height) {
	var result = new Array(height);
	for (var i = 0; i < height; ++i) {
		result[i] = new Array(width);
	}
	return result;
} 

function transposeMatrix(matrix) {
	if (matrix.length == 0) {
		return [];
	}
	var matrixRow = matrix[0];
	if (matrixRow == null) {
		throw new Error('Undefined matrix row 1.');
	}
	var matrixWidth = matrixRow.length;
	var matrixHeight = matrix.length;
	var result = createMatrix(matrixWidth, matrixHeight);
	for (var i = 0; i < matrixHeight; ++i) {
		var matrixRow = matrix[i];
      	if (matrixRow == null) {
			throw new Error('Undefined matrix row ' + (i + 1) + '.');
		}
		if (matrixWidth != matrixRow.length) {
			throw new Error('Matrix row ' + (i + 1) + ' has different width than row 1.');
		}
		for (var j = 0; j < matrixWidth; ++j) {
			result[j][i] = matrixRow[j];
		}
	}
	return result;
}

// Helper methods:

function printMatrix(matrix) {
	var text = '';
	for (var i = 0; i < matrix.length; ++i) {
		var row = matrix[i];
		for (var j = 0; j < row.length; ++j) {
			text += row[j] + ' ';
		}
		text += '\n';
	}
	console.log(text);
}

// Usage example:

var matrix = [
	[1, 2, 3],
	[4, 5, 6],
	[7, 8, 9]
];
var transposedMatrix = transposeMatrix(matrix);

printMatrix(transposedMatrix); // 1 4 7 
                               // 2 5 8 
                               // 3 6 9 

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more