Scilab Home page | Wiki | Bug tracker | Forge | Mailing list archives | ATOMS | File exchange
Please login or create an account
Change language to: Français - Português - 日本語 - Русский

Please note that the recommended version of Scilab is 6.0.1. This page might be outdated.
See the recommended documentation of this function

Scilab Help >> Elementary Functions > Matrix manipulation > resize_matrix


trim or/and extend (and cast) a matrix or hypermatrix

Calling Sequence

resMat = resize_matrix(mat, nbRows, nbCols)

resMat = resize_matrix(mat, nbRows, nbCols, resType)
resMat = resize_matrix(mat, nbRows, nbCols, resType, padding)
resMat = resize_matrix(mat, nbRows, nbCols,    ""  , padding)

resMat = resize_matrix(mat, newSizes)
resMat = resize_matrix(mat, newSizes, resType)
resMat = resize_matrix(mat, newSizes, resType, padding)
resMat = resize_matrix(mat, newSizes,    ""  , padding)



input matrix or hypermatrix. booleans, encoded integers, decimals (real or complexes), polynomials, or text are supported.


new number of rows of the resized matrix. Exceeding rows are trimmed. Missing rows are padded


new number of columns of the resized matrix. Exceeding columns are trimmed. Missing columns are padded


vector specifying the new sizes along each dimension of mat.

If it is shorter than size(mat), it is padded with ones. Example: if mat with size(mat)==[ 4 3 3 2] is provided and newSizes=[6 2] is specified, newSizes = [6 2 1 1] is considered.

If newSizes is longer than size(mat), new dimensions are added to mat and padded. Example: if mat such that size(mat)==[ 4 3 ] is provided and newSizes=[6 2 2] is specified, the result will be an hypermatrix with 2 pages, the second one being fully padded.

newSizes = [nbRows, nbCols] may be used for a matrix.


optional scalar of same type as mat, specifying the content to set in elements created when the size along a dimension is increased. The default padding is done with 0 (real or complex decimals, encoded integers, polynomials), or "" (text), or %F (booleans).

When mat and padding types do not match, scilab tries to convert the padding's one

For polynomials, the varname of the padding is forced to the mat's one.


optional text word specifying the data type into which the resized matrix must be converted. "boolean", "constant", "string", "int8", "uint8", "int16", "uint16", "int32", and "uint32" are supported.

Type conversion is supported neither for Polynomials nor for hypermatrix of text.


resized (and converted) matrix or hypermatrix


Creates a matrix of sizes [nbRows, nbCols] or newSizes, or an hypermatrix of sizes newSizes. If for a dimension the new size is smaller than the initial one, the matrix is cropped. If the size is increased, the matrix/hypermatrix is padded. The number of dimensions can be increased. Respectively, Scilab automatically squeezes highest dimensions with size kept or set to 1 (singletons).

The type of the result may be changed by specifying the resType argument, with restrictions given above.


// Embedded examples, including with polynomials:

// Numerical matrix:
M = grand(4, 3, "uin", -9, 9)
resize_matrix(M, 3, 4)
resize_matrix(M, [3 4 2])
resize_matrix(M, [3 4 2], "", %i)
resize_matrix(M, [3 4 2], "string", %i)

// Matrix of text:
myMatString = ["Scilab", "the"; "Open Source", "Scientific"; "Software", "Package"]
resize_matrix( myMatString, 5, 3 )
// Equivalent syntax for new sizes:
resize_matrix( myMatString, [5 3], "", "$" )    // With custom padding

// Crops, pads and casts an hypermatrix:
h = rand(2, 3, 2)*200
resize_matrix(h, [3 2 3], "int8")
resize_matrix(h, [3 2 3], "int8", -1)    // Custom padding
r = resize_matrix(h, [3 2 ] , "" , -1)   // Custom padding without type conversion
size(r)  // The last dimension has been squeezed

// With Polynomials:
x = poly(0, "x");
P = (1-x)^grand(4, 2, "uin", 0, 3)
resize_matrix(P, 3, 3)
resize_matrix(P, [3 3 2])
resize_matrix(P, [3 3 2], "", %z)
//  => The padding's unknown is forced to the P's one
//  => Polynomials can't be converted

See Also

  • matrix — reshape a vector or a matrix to a different size matrix
  • size — size of objects
  • typeof — object type
  • double — conversion from integer to double precision representation
  • strtod — convert string to double
  • string — convert string to double


5.5.0 Polynomials and Hypermatrices are now accepted. Custom padding can now be provided. New sizes can be specified in a vector. resize_matrix with no parameters displays examples (demo).
Scilab Enterprises
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Fri Apr 11 14:06:50 CEST 2014