Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试根据以下条件在 MATLAB 中定义一个函数:
If t<0 f(t,x,y)=t*(x/y)+1. else f(t,x,y)=-t*(x/y)+1. end
我找到了一种在一个变量中定义分段函数的方法,但这里我有三个变量。有没有办法在 MATLAB 中定义这样的函数?
如果我理解正确,你需要做 3if秒。我将向您展示如何为 2 个变量执行此操作:
if
If t<0 if x<0 %Case 1 else %Case 2 end else if x<0 %Case 3 else %Case 4 end end
或者,您可以使用 2^3=8 if- elseifs。或者,在 2 个变量的情况下 - 2^2 = 4。
elseif
if t<0 && x<0 %Case 1 elseif t<0 && x>0 %Case 2 elseif t>0 && x>0 %Case 3 else %Case 4 end
以下使用您上面描述的等式创建一个匿名函数
f = @(t,x,y) -abs(t) * (x/y) + 1;
然后你可以像普通函数一样使用它:
y = f(tData,xData,yData);
如果比这更复杂,那么它需要是子函数、嵌套函数或私有函数。