20.08.2015 Views

MATLAB array manipulation tips and tricks

MATLAB array manipulation tips and tricks - Home - Online.no

MATLAB array manipulation tips and tricks - Home - Online.no

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.

CHAPTER 3. BASIC ARRAY PROPERTIES 7which is the essential part of the function mttsize in the MTT Toolbox.Code like the following is sometimes seen, unfortunately. It might be more intuitive than theabove, but it is more fragile since it might use a lot of memory when dims contains a large value.sx = size(x);% get size along all dimensionsn = max(dims(:)) - ndims(x); % number of dimensions to appendsx = [ sx ones(1, n) ];% pad size vectorsiz = sx(dims);% extract dimensions of interestAn unlikely scenario perhaps, but imagine what happens if x <strong>and</strong> dims both are scalars <strong>and</strong> thatdims is a billion. The above code would require more than 8 GB of memory. The suggestedsolution further above requires a negligible amount of memory. There is no reason to write fragilecode when it can easily be avoided.3.2 Dimensions3.2.1 Number of dimensionsThe number of dimensions of an <strong>array</strong> is the number of the highest non-singleton dimension (seesection 3.2.2) but never less than two since <strong>array</strong>s in <strong>MATLAB</strong> always have at least two dimensions.The function which returns the number of dimensions is ndims, so the number of dimensions of an<strong>array</strong> x isdx = ndims(x);% number of dimensionsOne may also say that ndims(x) is the largest value of dim, no less than two, for which size(x,dim)is different from one.Here are a few examplesx = ones(2,1)x = ones(2,1,1,1)x = ones(1,0)x = ones(1,2,3,0,0)x = ones(2,3,0,0,1)x = ones(3,0,0,1,2)% 2-dimensional% 2-dimensional% 2-dimensional% 5-dimensional% 4-dimensional% 5-dimensional3.2.2 Singleton dimensionsA “singleton dimension” is a dimension along which the length is one. That is, if size(x,dim)is one, then dim is a singleton dimension. If, in addition, dim is larger than ndims(x), then dimis called a “trailing singleton dimension”. Trailing singleton dimensions are ignored by size <strong>and</strong>ndims.Singleton dimensions may be removed with squeeze. Removing singleton dimensions changesthe size of an <strong>array</strong>, but it does not change the number of elements in an <strong>array</strong>Flipping an <strong>array</strong> along a singleton dimension is a null-operation, that is, it has no effect, itchanges nothing.3.3 Number of elementsThe number of elements in an <strong>array</strong> may be obtained with numel, e.g., numel(x) is the numberof elements in x. The number of elements is simply the product of the length along all dimensions,that is, prod(size(x)).

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

Saved successfully!

Ooh no, something went wrong!