Within a function, how do I create variables that will accept 2 equally sized matrices and a scalar?
function[f] = get_f(y,Q,L)
Q = zeros(2) % creating a 2x2 matrix of zeros
L = diag(zeros(2)) % creating a diag开发者_高级运维onal matrix
% still playing with how I can pull y in as a scalar, I'm thinking I have
% to assign it earlier in the script where I call this function.
f = expm((Q-L).^y).*L % execution of the function itself
How do I tell the function to look for an entered scalar, and 2 equally sized matrices, then execute the listed command?
In your function, y
is whatever you put as the first argument in your function call.
For instance:
get_f(3.14, [1 2; 3 4], [1 0; 0 1])
calls the function get_f
with
y = 3.14
Q = [1 2; 3 4]
L = [1 0; 0 1]
so your function will work.
However, if you want your function to fail if y
is not a scalar or if Q
and L
don't have the same size, you can add a condition like this at the beginning of your function:
if ~isscalar(y)
error('y must be a scalar')
end
if any(size(Q) ~= size(L))
error('Q and L must have the same size')
end
精彩评论