12.07.2015 Views

GNU Octave - Local Sector 7 web page

GNU Octave - Local Sector 7 web page

GNU Octave - Local Sector 7 web page

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

66 <strong>GNU</strong> <strong>Octave</strong>warn resize on range errorBuilt-in VariableIf the value of warn_resize_on_range_error is nonzero, print a warning when amatrix is resized by an indexed assignment with indices outside the current bounds.The default value is 0.Note that it is quite inefficient to create a vector using a loop like the one shown in theexample above. In this particular case, it would have been much more efficient to use theexpressiona = sqrt (1:10);thus avoiding the loop entirely. In cases where a loop is still required, or a number of valuesmust be combined to form a larger matrix, it is generally much faster to set the size ofthe matrix first, and then insert elements using indexing commands. For example, given amatrix a,[nr, nc] = size (a);x = zeros (nr, n * nc);for i = 1:nx(:,(i-1)*nc+1:i*nc) = a;endforis considerably faster thanx = a;for i = 1:n-1x = [x, a];endforparticularly for large matrices because <strong>Octave</strong> does not have to repeatedly resize the result.10.2 Calling FunctionsA function is a name for a particular calculation. Because it has a name, you can askfor it by name at any point in the program. For example, the function sqrt computes thesquare root of a number.A fixed set of functions are built-in, which means they are available in every <strong>Octave</strong>program. The sqrt function is one of these. In addition, you can define your own functions.See Chapter 13 [Functions and Scripts], <strong>page</strong> 91, for information about how to do this.The way to use a function is with a function call expression, which consists of the functionname followed by a list of arguments in parentheses. The arguments are expressions whichgive the raw materials for the calculation that the function will do. When there is morethan one argument, they are separated by commas. If there are no arguments, you canomit the parentheses, but it is a good idea to include them anyway, to clearly indicate thata function call was intended. Here are some examples:sqrt (x^2 + y^2)ones (n, m)rand ()# One argument# Two arguments# No argumentsEach function expects a particular number of arguments. For example, the sqrt functionmust be called with a single argument, the number to take the square root of:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!