Home > FMAToolbox > General > ExtendArray.m

ExtendArray

PURPOSE ^

ExtendArray - Extend existing array to a given (larger) size.

SYNOPSIS ^

function y = ExtendArray(x,s,fill)

DESCRIPTION ^

ExtendArray - Extend existing array to a given (larger) size.

  USAGE

    y = ExtendArray(x,s)

    x              input array
    s              output size, must have the same number of dimensions as x
    fill           optional value for new elements (default: nan)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function y = ExtendArray(x,s,fill)
0002 
0003 %ExtendArray - Extend existing array to a given (larger) size.
0004 %
0005 %  USAGE
0006 %
0007 %    y = ExtendArray(x,s)
0008 %
0009 %    x              input array
0010 %    s              output size, must have the same number of dimensions as x
0011 %    fill           optional value for new elements (default: nan)
0012 %
0013 
0014 % Copyright (C) 2013 by Michaƫl Zugaro
0015 %
0016 % This program is free software; you can redistribute it and/or modify
0017 % it under the terms of the GNU General Public License as published by
0018 % the Free Software Foundation; either version 3 of the License, or
0019 % (at your option) any later version.
0020 
0021 % Check inputs
0022 if nargin < 2,
0023     error('Incorrect number of parameters (type ''help <a href="matlab:help ExtendArray">ExtendArray</a>'' for details).');
0024 end
0025 currentSize = size(x);
0026 if length(currentSize) ~= length(s),
0027     error('Array and size do not have the same number of dimensions (type ''help <a href="matlab:help ExtendArray">ExtendArray</a>'' for details).');
0028 end
0029 
0030 % Create extended array, fill with requested value
0031 if nargin < 3,
0032     y = nan(s);
0033 else
0034     if ~isscalar(fill),
0035         error('Incorrect fill value (type ''help <a href="matlab:help ExtendArray">ExtendArray</a>'' for details).');
0036     end
0037     switch(fill),
0038         case 0,
0039             y = zeros(s);
0040         case 1,
0041             y = ones(s);
0042         case inf,
0043             y = inf(s);
0044         otherwise,
0045             y = fill*ones(s);
0046     end
0047 end
0048 
0049 % Copy old array into new array
0050 indices = arrayfun(@(x) 1:x,currentSize,'uniformoutput',0);
0051 y(indices{:}) = x;

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005