matrix-transpose
v1.0.3
Published
Transposes a matrix by switching the row and column indices of a multidimensional array.
Downloads
38
Maintainers
Readme
matrix-transpose
Transposes a matrix by switching the row and column indices of a multidimensional array:
transpose(array)
In other words, it flips a matrix over its diagonal. Inspired by the Replit.
Example
const { transpose } = require('matrix-transpose');
transpose([
[1, 2],
[3, 4],
[5, 6],
]);
Output:
[
[1, 3, 5],
[2, 4, 6]
]
Install
NPM:
npm install matrix-transpose --save
Yarn:
yarn add matrix-transpose
CDN:
<script src="https://unpkg.com/matrix-transpose@latest/umd/matrix-transpose.min.js"></script>
<script>
window.MatrixTranspose.transpose(/* array */);
</script>
Usage
Import module:
// ES Modules
import { transpose } from 'matrix-transpose';
// CommonJS
const { transpose } = require('matrix-transpose');
Transpose matrix:
transpose([
[1, 2],
[3, 4],
[5, 6],
]);
Output:
[
[1, 3, 5],
[2, 4, 6]
]
Transpose matrix with inconsistent column lengths:
transpose([[1], [2, 3], [4, 5, 6]]);
Output:
[
[1, 2, 4],
[, 3, 5],
[, , 6]
]
Options
excludeEmpty
When option excludeEmpty
is set to true
, then empty items are excluded:
transpose([[1], [2, 3], [4, 5, 6]], { excludeEmpty: true });
Output:
[[1, 2, 4], [3, 5], [6]]
Testing
Run tests with coverage:
npm test
Run tests in watch mode:
npm run test:watch
Lint files:
npm run lint
Fix lint errors:
npm run lint:fix
Release
Release and publish are automated by Release Please.