Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector to matrix in Octave

Tags:

octave

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?

like image 380
Timofey Gorshkov Avatar asked May 03 '12 13:05

Timofey Gorshkov


People also ask

How do you transpose a vector in Octave?

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.

How do you divide vectors in Octave?

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.


1 Answers

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.

like image 124
Matt Ball Avatar answered Jan 02 '23 19:01

Matt Ball