matrix
reshapes an array with the same number and order of components
Syntax
y = matrix(v, m, n) y = matrix(v, m1, m2, m3, ..) y = matrix(v, [sizes])
Arguments
- v
Any matricial container (regular matrix of any data type; cells array; structures array), of any number of dimensions (vector, matrix, hyperarray), with any sizes.
- n, m, m1, m2, ..
positive integers: new expected sizes after reshaping. One of them may be set to
-1.- sizes
vector of positive integers: new expected sizes
- y
the reshaped array, with
y(i)==v(i)andsize(y,"*")==size(v,"*").
Description
For an array or hyperarray v with n x
m entries, y = matrix(v, n, m) or
y = matrix(v, [n,m]) transforms v into a
n x m matrix, by stacking columnwise
the entries of v.
Similarly, for an array or hyperarray v with m1 x
m2 x m3 .. entries,
y = matrix(v, m1, m2, m3,..) or
y = matrix(v, [m1 m2 m3..]) transforms v
into a hyperarray with m1 rows, m2 columns,
m3 .. layers, ...etc.
In all cases, the linear order of components in y is the same as in
v: For any 1 <= i <= size(v,"*"),
y(i)=v(i).
If one of the sizes m or n or m1
etc.. is equal to -1, its actual value is automatically set such that the total number
of components is unchanged.
![]() |
|
Examples
With a regular matrix of decimal numbers:
a = [1 3 5 7 9 11; 2 4 6 8 10 12] matrix(a, 4, 3) matrix(a, [3 4]) matrix(a, 3, -1) // into an hypermatrix: matrix(a, 3, 2, 2)
With texts:
t = strsplit("a":"x",1:23)' matrix(t,[3,8])
--> t = strsplit("a":"x",1:23)'
t =
!a b c d e f g h i j k l m n o p q r s t u v w x !
--> matrix(t,[3,8])
ans =
!a d g j m p s v !
!b e h k n q t w !
!c f i l o r u x !
With a cells hyperarray:
c = makecell([2,3,2], %t,%z, %i,"abc", list(%pi,"a"), int8(23),cos,sind,"àèìòù",corelib, 0.5, 1/%z^2) rc = matrix(c, 3, 4) rc(8)==c(8) rc{8}.getshell
c =
(:,:,1)
[1x1 boolean] [1x1 polynomial] [1x1 constant]
[1x1 string ] [ list ] [1x1 int8 ]
(:,:,2)
[ 1 fptr ] [ 1 function] [1x1 string]
[ library] [ 1x1 constant] [ r ]
--> rc = matrix(c, 3, 4)
rc =
[1x1 boolean ] [ list ] [ 1 fptr ] [1x1 constant]
[1x1 string ] [1x1 constant] [ library ] [1x1 string ]
[1x1 polynomial] [1x1 int8 ] [ 1 function] [ r ]
--> rc(8)==c(8)
ans =
T
--> rc{8}.getshell
ans =
cmd
With a structures array:
clear s s(3,6).r = %pi matrix(s,2,9)
--> s(3,6).r = %pi
s =
3x6 struct array with fields:
r
--> matrix(s,2,9)
ans =
2x9 struct array with fields:
r
See also
- colon (:) — Ranging operator. Addresses all elements along an array dimension or of a list.
- resize_matrix — trim or/and extend (and cast) a matrix or hypermatrix
- ndims — number of dimensions of an array
- size — size of objects
- extraction — matrix and list entry extraction
- transposition — (') transpose operator, string delimiter
- brackets [..] — Concatenation. Recipients of an assignment. Results of a function
- matrices — Scilab objects, matrices in Scilab
- hypermatrices — a Scilab object, N dimensional matrix in Scilab
- makecell — Creates a cell array.
- struct — Builds a structure or an array of structures
- mlist — Scilab object, matrix oriented typed list definition
| Report an issue | ||
| << flipdim | Matrix - shaping | permute >> |
