.. highlightlang:: us .. _functions-for-vectors-and-matrices: Functions for Vectors and Matrices ================================== This chapter introduces some important vector and matrix functions. More information about these functions is available through the online help system. The :ref:`zeros` and :ref:`ones` functions create a vector or a matrix where all elements have the value 0 or 1. The function call ``zeros(nr, nc)`` returns a zero matrix with nr rows and nc columns. ``zeros(1, 10) + 1.0`` is equivalent to the expression ``ones(1, 10)``. The :ref:`rand` function (``rand(nr, nc)``) function returns a matrix with random numbers in the range 0 to 1. The ``size(x)`` function returns the size of the variable ``x``. The size is a 1 * 2 vector. The first element is the number of rows and the second is the number of columns. Example: :: * size(rand(3, 4)) 3 4 The function :ref:`nr` returns the number of rows of a matrix or a vector. The function :ref:`nc` returns the number of columns of a matrix or a vector. The function :ref:`len` returns the number of elements of a matrix or vector. The expression ``len(x) == nr(x) * nc(x)`` is true. Some examples: :: v[len(v):1] = v[1:len(v)] changes the element order of a vector. In the same way, the statement :: m[1:nr(m);] = m[nr(m):1;] changes the row order of a matrix and :: m[;1:nc(m)] = m[;nc(m):1] changes the column order. The statement :: m = m[; 2:nc(m), 1] "rotates" the matrix columns, i.e. moves the first column behind the last column of the matrix ``m``. :: v = v[1:3-1, 3+1:len(v)] removes the 3rd element from a vector. The following statement removes the first element of a vector :: v = v[2:len(v)] and :: v = v[1:len(v)-1] removes the last element. The matrix data type can be retrieved with the function :ref:`type`. The function returns one of the following strings: ``"real"``, ``"complex"``, ``"string"``, ``"undef"``, or ``"function"``. The :ref:`min` function returns the minimum of its argument vector. If the argrument is a matrix, :ref:`min` returns a row vector with the minimum of the matrix columns. The :ref:`max` function returns the maximum of a vector or matrix: :: * m = rand(2,2) * m 0.3237 0.2887 0.3244 0.3530 * min(m) 0.3237 0.1540 * max(m) 0.4934 0.3530 Double use of the function returns the smallest or largest of all matrix elements: ``min(min(m))`` or ``max(max(m))``. The :ref:`sort` function can sort the elements of a vector or a matrix. If the function argument is a matrix, all rows will be sorted by the first column. :: * srand() // Sets the seed to the standard value 1.0000 * m = rand(3,4) * m 0.7744 0.2484 0.1606 0.6082 0.3925 0.7937 0.4888 0.1344 0.6235 0.9313 0.6486 0.8489 * sort(m) 0.3925 0.7937 0.4888 0.1344 0.6235 0.9313 0.6486 0.8489 0.7744 0.2484 0.1606 0.6082 The :ref:`find` function returns the indices of all values which are not equal to 0. :ref:`find` is mainly used in conjunction with comparison operators:: * a = [1,7,5,22] * a <= 5 1.0000 0.0000 1.0000 0.0000 * find(a <= 5) 1.0000 3.0000 The next statement will set all elements of the vector ``a`` which are less than 5 to the value 9:: a[find(a < 5)] = 9; :sub:`id-1594549`