Home > matlab > missing > bsxfun > bsxfun.m

bsxfun

PURPOSE ^

(Imperfect) Clone of matlab's bsxfun built-in function.

SYNOPSIS ^

function C = bsxfun(fun,A,B)

DESCRIPTION ^

 (Imperfect) Clone of matlab's bsxfun built-in function.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function C = bsxfun(fun,A,B)
0002 % (Imperfect) Clone of matlab's bsxfun built-in function.
0003 
0004 % Copyright (C) 2010-2011 Dynare Team
0005 %
0006 % This file is part of Dynare.
0007 %
0008 % Dynare is free software: you can redistribute it and/or modify
0009 % it under the terms of the GNU General Public License as published by
0010 % the Free Software Foundation, either version 3 of the License, or
0011 % (at your option) any later version.
0012 %
0013 % Dynare is distributed in the hope that it will be useful,
0014 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0015 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016 % GNU General Public License for more details.
0017 %
0018 % You should have received a copy of the GNU General Public License
0019 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0020 
0021 dA = size(A);
0022 dB = size(B);
0023 
0024 dim_correction = length(dA)-length(dB);
0025 if dim_correction>0
0026     dB = [dB,ones(1,dim_correction)];
0027 elseif dim_correction<0
0028     dA = [dA,ones(1,-dim_correction)];
0029 end
0030 
0031 if all(dA==dB)
0032     C = fun(A,B);
0033 else
0034     tB = dB<dA;
0035     tA = dA<dB;
0036     if any(tB)
0037         iB = find(tB);
0038         if all(dB(iB)==1)
0039             B = repmat(B,tB.*dA+~tB);
0040         else
0041             error('Non-singleton dimensions of the two input arrays must match each other.')
0042         end
0043     end
0044     if any(tA)
0045         iA = find(tA);
0046         if all(dA(iA)==1)
0047             A = repmat(A,tA.*dB+~tA);
0048         else
0049             error('Non-singleton dimensions of the two input arrays must match each other.')
0050         end
0051     end
0052     C = fun(A,B);
0053 end

Generated on Tue 22-May-2012 02:40:23 by m2html © 2005