https://github.com/remarkablemark/matrix-transpose
Transposes a matrix by switching the row and column indices of a multidimensional array.
https://github.com/remarkablemark/matrix-transpose
associative-array javascript matrix nodejs npm transpose typescript
Last synced: 3 months ago
JSON representation
Transposes a matrix by switching the row and column indices of a multidimensional array.
- Host: GitHub
- URL: https://github.com/remarkablemark/matrix-transpose
- Owner: remarkablemark
- License: mit
- Created: 2020-12-28T21:50:11.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-10-29T09:10:35.000Z (7 months ago)
- Last Synced: 2024-10-29T11:33:13.690Z (7 months ago)
- Topics: associative-array, javascript, matrix, nodejs, npm, transpose, typescript
- Language: JavaScript
- Homepage: https://npmjs.com/package/matrix-transpose
- Size: 771 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# matrix-transpose
[](https://nodei.co/npm/matrix-transpose/)
[](https://www.npmjs.com/package/matrix-transpose)
[](https://github.com/remarkablemark/matrix-transpose/actions?query=workflow%3Abuild)
[](https://coveralls.io/github/remarkablemark/matrix-transpose?branch=master)[Transposes](https://wikipedia.org/wiki/Transpose) 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](https://replit.com/@remarkablemark/Swap-array-row-and-column).
#### Example
```js
const { transpose } = require('matrix-transpose');transpose([
[1, 2],
[3, 4],
[5, 6],
]);
```Output:
```json
[
[1, 3, 5],
[2, 4, 6]
]
```[Replit](https://replit.com/@remarkablemark/matrix-transpose) | [JSFiddle](https://jsfiddle.net/remarkablemark/c5upm1bz/)
## Install
[NPM](https://www.npmjs.com/package/matrix-transpose):
```sh
npm install matrix-transpose --save
```[Yarn](https://yarnpkg.com/package/matrix-transpose):
```sh
yarn add matrix-transpose
```[CDN](https://unpkg.com/matrix-transpose/):
```html
window.MatrixTranspose.transpose(/* array */);
```
## Usage
Import module:
```js
// ES Modules
import { transpose } from 'matrix-transpose';// CommonJS
const { transpose } = require('matrix-transpose');
```Transpose matrix:
```js
transpose([
[1, 2],
[3, 4],
[5, 6],
]);
```Output:
```json
[
[1, 3, 5],
[2, 4, 6]
]
```Transpose matrix with inconsistent column lengths:
```js
transpose([[1], [2, 3], [4, 5, 6]]);
```Output:
```json
[
[1, 2, 4],
[, 3, 5],
[, , 6]
]
```### Options
#### excludeEmpty
When option `excludeEmpty` is set to `true`, then empty items are excluded:
```js
transpose([[1], [2, 3], [4, 5, 6]], { excludeEmpty: true });
```Output:
```json
[[1, 2, 4], [3, 5], [6]]
```## Testing
Run tests with coverage:
```sh
npm test
```Run tests in watch mode:
```sh
npm run test:watch
```Lint files:
```sh
npm run lint
```Fix lint errors:
```sh
npm run lint:fix
```## Release
Release and publish are automated by [Release Please](https://github.com/googleapis/release-please).
## License
[MIT](https://github.com/remarkablemark/matrix-transpose/blob/master/LICENSE)