matrix-transpose
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

matrix-transpose

NPM

NPM version Build Status Coverage Status

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 Repl.it.

Example

const { transpose } = require('matrix-transpose');

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

Output:

[
  [1, 3, 5],
  [2, 4, 6]
]

Repl.it | JSFiddle

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

Only collaborators with credentials can release and publish:

$ npm run release
$ git push --follow-tags && npm publish

License

MIT

Package Sidebar

Install

npm i matrix-transpose

Weekly Downloads

17

Version

1.0.2

License

MIT

Unpacked Size

11.9 kB

Total Files

10

Last publish

Collaborators

  • remarkablemark