Skip to contents

Reshapes a matrix according to a certain number of dimensions

Usage

reshape(A, sz)

Arguments

A

input matrix

sz

vector containing the dimensions of the output vector

Value

the input matrix, reshaped according to the vector sz

Details

This function replicates the functionality of the reshape() function on Matlab. This function is basically a fancy wrapper for the array() function in R, but is useful because it saves the user translation time. Moreover, it introduces validation code that alter the behavior of array() and makes it more similar to replicate().

Note

The Matlab function also accepts as input the dismemberment of sz as scalars.

Examples

mx <- matrix(1:4, 2)
ra <- array(1:12, c(2, 3, 2))

mx
#>      [,1] [,2]
#> [1,]    1    3
#> [2,]    2    4
reshape(mx, c(1, 4))
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4

ra
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]    1    3    5
#> [2,]    2    4    6
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]    7    9   11
#> [2,]    8   10   12
#> 
reshape(ra, c(3, 2, 2))
#> , , 1
#> 
#>      [,1] [,2]
#> [1,]    1    4
#> [2,]    2    5
#> [3,]    3    6
#> 
#> , , 2
#> 
#>      [,1] [,2]
#> [1,]    7   10
#> [2,]    8   11
#> [3,]    9   12
#>