0001 function C = bsxfun(fun,A,B)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
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