What is the inverse operation to vec
in Octave?
E.g. if I need to convert 12x1 vector into 3x4 matrix, what should I do?
To interchange rows with columns, that is, to find the transpose of a vector or a matrix, use the apostrophe. For example, the command octave#:#> C = [4 7.5 -1]' will transform the row vector C = [4 7.5 -1] into a column vector.
The dot in front of operators such as * / and ^ means it's is an element-by-element operation. Of course, you can always perform operations on individual elements of a vector or matrix. For example, octave#:#> X(2)/Y(3) will divide the second element of vector X by the third element of Y.
I don't know Octave, but I think you're looking for reshape
.
— Built-in Function: reshape (A, m, n, ...)
— Built-in Function: reshape (A, [m n ...])
— Built-in Function: reshape (A, ..., [], ...)
— Built-in Function: reshape (A, size)Return a matrix with the specified dimensions (m, n, ...) whose elements are taken from the matrix A. The elements of the matrix are accessed in column-major order (like Fortran arrays are stored).
The following code demonstrates reshaping a 1x4 row vector into a 2x2 square matrix.
reshape ([1, 2, 3, 4], 2, 2) ⇒ 1 3 2 4
Note that the total number of elements in the original matrix (
prod (size (A))
) must match the total number of elements in the new matrix (prod ([
m n ...])
).A single dimension of the return matrix may be left unspecified and Octave will determine its size automatically. An empty matrix ([]) is used to flag the unspecified dimension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With