Skip to content

Latest commit

 

History

History
155 lines (97 loc) · 4.11 KB

File metadata and controls

155 lines (97 loc) · 4.11 KB

rotr90

Rotate a matrix (or a stack of matrices) 90 degrees clockwise.

Usage

var rotr90 = require( '@stdlib/ndarray/base/rotr90' );

rotr90( x, k, writable )

Rotates a matrix (or a stack of matrices) 90 degrees clockwise.

var array = require( '@stdlib/ndarray/array' );

var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
// returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]

var y = rotr90( x, 1, false );
// returns <ndarray>[ [ 3, 1 ], [ 4, 2 ] ]

The function accepts the following arguments:

  • x: input ndarray.
  • k: number of times to rotate by 90 degrees. Positive values rotate clockwise. Negative values rotate counterclockwise.
  • writable: boolean indicating whether a returned ndarray should be writable.

Notes

  • The writable parameter only applies to ndarray constructors supporting read-only instances.
  • If k > 0 the function rotates the matrix clockwise.
  • If k < 0 the function rotates the matrix counterclockwise.
  • The returned ndarray is a view of the input ndarray. Accordingly, writing to the original ndarray will mutate the returned ndarray and vice versa.
  • If provided an ndarray with fewer than two dimensions, the function returns the input array unchanged.

Examples

var array = require( '@stdlib/ndarray/array' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var rotr90 = require( '@stdlib/ndarray/base/rotr90' );

// Create a 2x3 matrix:
var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );

// Rotate 90 degrees clockwise:
var y = rotr90( x, 1, false );
var arr = ndarray2array( y );
// returns [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ]

// Rotate 180 degrees:
y = rotr90( x, 2, false );
arr = ndarray2array( y );
// returns [ [ 6, 5, 4 ], [ 3, 2, 1 ] ]

// Rotate 270 degrees clockwise (equivalent to 90 degrees counterclockwise):
y = rotr90( x, 3, false );
arr = ndarray2array( y );
// returns [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ]

// Rotate 360 degrees (equivalent to no rotation):
y = rotr90( x, 4, false );
arr = ndarray2array( y );
// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]

// Rotate 90 degrees counterclockwise (equivalent to 270 degrees clockwise):
y = rotr90( x, -1, false );
arr = ndarray2array( y );
// returns [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ]