Matlab 在分段函数中计算向量

Matlab 在分段函数中计算向量,matlab,data-fitting,model-fitting,piecewise,Matlab,Data Fitting,Model Fitting,Piecewise,我想在分段函数中计算向量扩展数据,如下所示: [theta] = lsqcurvefit(@y, p0, xdata, ydata); if [1 3 -1] > 0 %code end dat=荷载('k2.txt'); 扩展数据=数据(:,1); ydata=dat(:,2); n=长度(扩展数据); p0=[0.08216.60.4]; y(p0,扩展数据) 函数y_hat=y(p,t) P=4.885; T0=134.27426; ω=2*pi/P; gamma1=0

我想在分段函数中计算向量扩展数据,如下所示:


[theta] = lsqcurvefit(@y, p0, xdata, ydata);
if [1 3 -1] > 0
   %code
end

dat=荷载('k2.txt');
扩展数据=数据(:,1);
ydata=dat(:,2);
n=长度(扩展数据);
p0=[0.08216.60.4];
y(p0,扩展数据)
函数y_hat=y(p,t)
P=4.885;
T0=134.27426;
ω=2*pi/P;
gamma1=0.3539;gamma2=0.2851;
c1=0;c2=gamma1+2*gamma2;c3=0;c4=-gamma2;
c0=1-c1-c2-c3-c4;
z=p(2)。*(sin(omega.*(t-T0))。^2+((p(3)/p(2))。*cos(omega.*(t-T0))。^2)。^(1/2);
lambda1=0;
lambda3=p(1)。^2;

如果((1-p(1)您的代码不工作,因为当您编写这样的代码时:


[theta] = lsqcurvefit(@y, p0, xdata, ydata);
if [1 3 -1] > 0
   %code
end
如果向量中的每个值的条件都成立,则不会计算“%code”,因为它正在检查

假设您要定义函数并计算其向量。然后,您要做的是使用for循环:

x_vals = [-1 1 5];
heav(x_vals)
function y = heav(x)
    y = zeros(size(x));
    for i = 1:length(x)
        if x(i) >= 0
            y(i) = 1;
        else
            y(i) = 0;
        end
    end
end

不,您不需要循环。您可以将该循环替换为
y(x>=0)=1