Octave – Gestione matrici – I – 82

img_20160920_070443

Capitolo nuovo, continuando da qui copio qui.

Manipolare matrici
There are a number of functions available for checking to see if the elements of a matrix meet some condition, and for rearranging the elements of a matrix. For example, Octave can easily tell you if all the elements of a matrix are finite, or are less than some specified value. Octave can also rotate the elements, extract the upper- or lower-triangular parts, or sort the columns of a matrix.

Pronti? via! sono qui.

Trovare elementi e verificare condizioni
The functions any and all are useful for determining whether any or all of the elements of a matrix satisfy some condition. The find function is also useful in determining which elements of a matrix meet a specified condition.

Built-in Function: any (x)
Built-in Function: any (x, dim)

For a vector argument, return true (logical 1) if any element of the vector is nonzero.
For a matrix argument, return a row vector of logical ones and zeros with each element indicating whether any of the elements of the corresponding column of the matrix are nonzero. For example:

o423

Built-in Function: all (x)
Built-in Function: all (x, dim)

For a vector argument, return true (logical 1) if all elements of the vector are nonzero.
For a matrix argument, return a row vector of logical ones and zeros with each element indicating whether all of the elements of the corresponding column of the matrix are nonzero. For example:

o424

If the optional argument dim is supplied, work along dimension dim.

Since the comparison operators (see Comparison Ops) return matrices of ones and zeros, it is easy to test a matrix for many things, not just whether the elements are nonzero. For example,

o425

Note that in conditional contexts (like the test clause of if and while statements) Octave treats the test as if you had typed all (all (condition)).

Function File: z = xor (x, y)
Function File: z = xor (x1, x2, ...)

Return the exclusive or of x and y.
For boolean expressions x and y, xor (x, y) is true if and only if one of x or y is true. Otherwise, if x and y are both true or both false, xor returns false.
If more than two arguments are given the xor operation is applied cumulatively from left to right: (...((x1 xor x2) xor x3) xor ...).

Built-in Function: diff (x)
Built-in Function: diff (x, k)
Built-in Function: diff (x, k, dim)

If x is a vector of length n, diff (x) is the vector of first differences x(2) - x(1), ..., x(n) - x(n-1).
If x is a matrix, diff (x) is the matrix of column differences along the first non-singleton dimension.
The second argument is optional. If supplied, diff (x, k), where k is a non-negative integer, returns the k-th differences. It is possible that k is larger than the first non-singleton dimension of the matrix. In this case, diff continues to take the differences along the next non-singleton dimension.
The dimension along which to take the difference can be explicitly stated with the optional variable dim. In this case the k-th order differences are calculated along this dimension. In the case where k exceeds size (x, dim) an empty matrix is returned.

o426

Mapping Function: isinf (x)
Return a logical array which is true where the elements of x are infinite and false where they are not.

o427

Mapping Function: isnan (x)
Return a logical array which is true where the elements of x are NaN values and false where they are not.
NA values are also considered NaN values.

o428

Function File: [err, y1, ...] = common_size (x1, ...)
Determine if all input arguments are either scalar or of common size.
If true, err is zero, and yi is a matrix of the common size with all entries equal to xi if this is a scalar or xi otherwise. If the inputs cannot be brought to a common size, err is 1, and yi is xi.

o429

This is useful for implementing functions where arguments can either be scalars or of common size.

Built-in Function: idx = find (x)
Built-in Function: idx = find (x, n)
Built-in Function: idx = find (x, n, direction)
Built-in Function: [i, j] = find (...)
Built-in Function: [i, j, v] = find (...)

Return a vector of indices of nonzero elements of a matrix, as a row if x is a row vector or as a column otherwise.
To obtain a single index for each matrix element, Octave pretends that the columns of a matrix form one long vector (like Fortran arrays are stored). For example:

o430

If two inputs are given, n indicates the maximum number of elements to find from the beginning of the matrix or vector.
If three inputs are given, direction should be one of "first" or "last", requesting only the first or last n indices, respectively. However, the indices are always returned in ascending order.
If two outputs are requested, find returns the row and column indices of nonzero elements of a matrix. For example:

o431

If three outputs are requested, find also returns a vector containing the nonzero values. For example:

o432

Note that this function is particularly useful for sparse matrices, as it extracts the nonzero elements as vectors, which can then be used to create the original matrix. For example:

o433

misteriosa come il fondo dell’oceano (cit.) 😦

Built-in Function: idx = lookup (table, y)
Built-in Function: idx = lookup (table, y, opt)

Lookup values in a sorted table.
This function is usually used as a prelude to interpolation.
If table is increasing and idx = lookup (table, y), then table(idx(i)) <= y(i) < table(idx(i+1)) for all y(i) within the table. If y(i) < table(1) then idx(i) is 0. If y(i) >= table(end) or isnan (y(i)) then idx(i) is n.
If the table
is decreasing, then the tests are reversed. For non-strictly monotonic tables, empty intervals are always skipped. The result is undefined if table is not monotonic, or if table contains a NaN.

The complexity of the lookup is O(M*log(N)) where N is the size of table and M is the size of y. In the special case when y is also sorted, the complexity is O(min(M*log(N),M+N)).
table and y can also be cell arrays of strings (or y can be a single string). In this case, string lookup is performed using lexicographical comparison.

If opts is specified, it must be a string with letters indicating additional option

  • m table(idx(i)) == val(i) if val(i) occurs in table; otherwise, idx(i) is zero.
  • b idx(i) is a logical 1 or 0, indicating whether val(i) is contained in table or not.
  • l For numeric lookups the leftmost subinterval shall be extended to infinity (i.e., all indices at least 1)
  • r For numeric lookups the rightmost subinterval shall be extended to infinity (i.e., all indices at most n-1).

o434

If you wish to check if a variable exists at all, instead of properties its elements may have, consult Status of Variables.

:mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: