Previous: User-defined Data Types, Up: Data Types [Contents][Index]

### 3.3 Object Sizes

The following functions allow you to determine the size of a variable or expression. These functions are defined for all objects. They return -1 when the operation doesn’t make sense. For example, Octave’s data structure type doesn’t have rows or columns, so the `rows` and `columns` functions return -1 for structure arguments.

ndims (a)

  Return the number of dimensions of a.

  For any array, the result will always be greater than or equal to 2. Trailing singleton dimensions are not counted.

      ndims (ones (4, 1, 2, 1))
          ⇒ 3

  See also: size.

columns (a)

  Return the number of columns of a.

  See also: rows, size, length, numel, isscalar, isvector, ismatrix.

rows (a)

  Return the number of rows of a.

  See also: columns, size, length, numel, isscalar, isvector, ismatrix.

numel (a)
numel (a, idx1, idx2, …)

  Return the number of elements in the object a.

  Optionally, if indices idx1, idx2, … are supplied, return the number of elements that would result from the indexing

      a(idx1, idx2, …)

  Note that the indices do not have to be scalar numbers. For example,

      a = 1;
      b = ones (2, 3);
      numel (a, b)

  will return 6, as this is the number of ways to index with b. Or the index could be the string `":"` which represents the colon operator. For example,

      a = ones (5, 3);
      numel (a, 2, ":")

  will return 3 as the second row has three column entries.

  This method is also called when an object appears as lvalue with cs-list indexing, i.e., `object{…}` or `object(…).field`.

  See also: size, length, ndims.

length (a)

  Return the length of the object a.

  The length is 0 for empty objects, 1 for scalars, and the number of elements for vectors. For matrix or N-dimensional objects, the length is the number of elements along the largest dimension (equivalent to `max (size (a))`).

  See also: numel, size.

sz = size (a)
dim_sz = size (a, dim)
[rows, cols, …, dim_N_sz] = size (…)

  Return a row vector with the size (number of elements) of each dimension for the object a.

  When given a second argument, dim, return the size of the corresponding dimension.

  With a single output argument, `size` returns a row vector. When called with multiple output arguments, `size` returns the size of dimension N in the Nth argument. The number of rows, dimension 1, is returned in the first argument, the number of columns, dimension 2, is returned in the second argument, etc. If there are more dimensions in a then there are output arguments, `size` returns the total number of elements in the remaining dimensions in the final output argument.

  Example 1: single row vector output

      size ([1, 2; 3, 4; 5, 6])
         ⇒ [ 3, 2 ]

  Example 2: number of elements in 2nd dimension (columns)

      size ([1, 2; 3, 4; 5, 6], 2)
          ⇒ 2

  Example 3: number of output arguments == number of dimensions

      [nr, nc] = size ([1, 2; 3, 4; 5, 6])
          ⇒ nr = 3
          ⇒ nc = 2

  Example 4: number of output arguments != number of dimensions

      [nr, remainder] = size (ones (2, 3, 4, 5))
          ⇒ nr = 2
          ⇒ remainder = 60

  See also: numel, ndims, length, rows, columns, size_equal, common_size.

isempty (a)

  Return true if a is an empty matrix (any one of its dimensions is zero).

  See also: isnull, isa.

isnull (x)

  Return true if x is a special null matrix, string, or single quoted string.

  Indexed assignment with such a null value on the right-hand side should delete array elements. This function is used in place of `isempty` when overloading the indexed assignment method (`subsasgn`) for user-defined classes. `isnull` is used to distinguish between these two cases:

  `A(I) = []`

  and

  `X = []; A(I) = X`

  In the first assignment, the right-hand side is `[]` which is a special null value. As long as the index I is not empty, this code should delete elements from A rather than perform assignment.

  In the second assignment, the right-hand side is empty (because X is `[]`), but it is not null. This code should assign the empty value to elements in A.

  An example from Octave’s built-in char class demonstrates the interpreter behavior when `isnull` is used correctly.

      str = "Hello World";
      nm = "Wally";
      str(7:end) = nm                # indexed assignment
        ⇒ str = Hello Wally
      str(7:end) = ""                # indexed deletion
        ⇒ str = Hello

  See also: isempty, isindex.

sizeof (val)

  Return the size of val in bytes.

  See also: whos.

size_equal (a, b, …)

  Return true if the dimensions of all arguments agree.

  Trailing singleton dimensions are ignored. When called with a single argument, or no argument, `size_equal` returns true.

  See also: size, numel, ndims, common_size.

squeeze (x)

  Remove singleton dimensions from x and return the result.

  Note that for compatibility with MATLAB, all objects have a minimum of two dimensions and row vectors are left unchanged.

  See also: reshape.

Previous: User-defined Data Types, Up: Data Types [Contents][Index]