bsxfun
Evaluates the function func on the elements (element-wise operation) of the input matrices.
Syntax
R = bsxfun(func, input1, input2)
Inputs
- func
- Function to be evaluated. The function must accept two scalar inputs.
- input1
- Matrix or scalar to be operated on.
- input2
- Matrix or scalar to be operated on.
Outputs
- R
- Resulting output.
Examples
Evaluate function on scalar and matrix inputs:
function z = my_fun(a,b)
z = 2*max(a,b)+b;
end
R = bsxfun(@my_fun, 2, [2 3 4;5 6 7])
R = [Matrix] 2 x 3
6 9 12
15 18 21
Evaluate function on matrix inputs:
function z = my_fun(a,b)
z = 2*max(a,b)+b;
end
R = bsxfun(@my_fun, [1 2 3;10 11 12], [2 3 4;5 6 7])
R = [Matrix] 2 x 3
6 9 12
25 28 31
Evaluate function on nd matrix and matrix inputs:
function z = my_fun(a,b)
z = 2*max(a,b)+b;
end
a = [1 2 3;4 5 6];
a(:,:,2) = ones(2, 3);
b = [2 3 4;5 6 7];
R = bsxfun(@my_fun, a, b)
R = [Matrix] 2 x 3
6 9 12
25 28 31
Comments
If both input1 and input2 are matrices then their dimensions must be the same.