How to resolve the algorithm Matrix transposition step by step in the JavaScript programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Matrix transposition step by step in the JavaScript programming language
Table of Contents
Problem Statement
Transpose an arbitrarily sized rectangular Matrix.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Matrix transposition step by step in the JavaScript programming language
First implementation:
- The
Matrix
class takes in an array as an argument and initialises itsmtx
property to the input array. - It also sets the
height
andwidth
properties of the matrix based on the input array. - The
toString
method returns a string representation of the matrix by joining the rows of the matrix with newlines and the elements of each row with commas. - The
transpose
method creates a new matrix by transposing the input matrix, i.e., swapping its rows and columns.
Second implementation:
- The
transpose
function takes in a list as an argument and returns a new list where each element is a list of the elements in the corresponding column of the input list. - It uses the
map
method to create a new list of lists, where each inner list is created by mapping the input list to the element at the corresponding column index.
Third implementation:
- The
transpose
function takes in a list of lists as an argument and returns a new list of lists where each element is a list of the elements in the corresponding column of the input list. - It uses the
map
method to create a new list of lists, where each inner list is created by mapping the input list to the element at the corresponding column index.
Usage:
- The code initialises a matrix object
m
with a 5x4 matrix of numbers. - It then prints the original matrix and its transpose.
- The second and third implementations are called with a 4x3 matrix of numbers and their results are printed.
Output:
[1,1,1,1]
[2,4,8,16]
[3,9,27,81]
[4,16,64,256]
[5,25,125,625]
[1,2,3,4,5]
[1,4,9,16,25]
[1,8,27,64,125]
[1,16,81,256,625]
[[1,4,7],[2,5,8],[3,6,9]]
Source code in the javascript programming language
function Matrix(ary) {
this.mtx = ary
this.height = ary.length;
this.width = ary[0].length;
}
Matrix.prototype.toString = function() {
var s = []
for (var i = 0; i < this.mtx.length; i++)
s.push( this.mtx[i].join(",") );
return s.join("\n");
}
// returns a new matrix
Matrix.prototype.transpose = function() {
var transposed = [];
for (var i = 0; i < this.width; i++) {
transposed[i] = [];
for (var j = 0; j < this.height; j++) {
transposed[i][j] = this.mtx[j][i];
}
}
return new Matrix(transposed);
}
var m = new Matrix([[1,1,1,1],[2,4,8,16],[3,9,27,81],[4,16,64,256],[5,25,125,625]]);
print(m);
print();
print(m.transpose());
(function () {
'use strict';
function transpose(lst) {
return lst[0].map(function (_, iCol) {
return lst.map(function (row) {
return row[iCol];
})
});
}
return transpose(
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
);
})();
(() => {
"use strict";
// transpose :: [[a]] -> [[a]]
const transpose = xs =>
0 < xs.length ? (
xs[0].map(
(_, iCol) => xs.map(
row => row[iCol]
)
)
) : [];
// ---------------------- TEST -----------------------
const main = () =>
JSON.stringify(
transpose([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
);
// MAIN ---
return main();
})();
[[1,4,7],[2,5,8],[3,6,9]]
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the Forth programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the Locomotive Basic programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the Miranda programming language
You may also check:How to resolve the algorithm Cantor set step by step in the Elixir programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the D programming language