让我详细说明一下我在你的帖子上发表的评论。
如果您想使用2个不同的函数和一个拆分从输入数组创建输出数组,可以使用类似以下代码示例中的数组索引范围。
x = randn(20,1);
threshold = 5;
y = zeros(size(x));
y(1:threshold) = fun1(x(1:threshold));
y(1+threshold:end) = fun2(x(1+threshold:end));
如果您愿意,可以跳过对
y
的预分配,并将附加数据直接连接到输出的末尾。如果函数返回的输出元素数量与输入元素数量不同,这将特别有用。以下是其语法示例。
y = fun1(x(1:threshold));
y = [y; fun2(x(1+threshold:end))];
编辑:
回应下面的帖子,这里有一个完整的例子...
(该文涉及IT技术)
clc; close all
x = -5:5;
y = [1 1 0 -1 -1 0 1 1 1 1 1];
t = -5:.01:5;
xIdx = 5;
tIdx = floor(numel(t)/numel(x)*xIdx);
out = pchip(x(1:xIdx),y(1:xIdx),t(1:tIdx));
out = [out spline(x((1+xIdx):end),y((1+xIdx):end),t((1+tIdx):end))];
plot(x,y,'o',t,out,'-',[x(xIdx) x(xIdx)], [-1.5 1.5], '-')
legend('data','output','split',4);
ylim ([-1.5 1.5])
Which will give . . .
![enter image description here](https://istack.dev59.com/9ZNgj.webp)
x
,你难道不能只是这样做吗?y(1:threshold)=interpA(1:threshold); y(1+threshold:end)=interpB(1+threshold:end);
?? - learnvst