在matlab中将2行与不同的自变量组合起来

时间:2016-07-06 16:47:02

标签: matlab

我有两组x和y变量。 x变量是不一样的,否则我只是添加y向量和绘图与x,但相反,我想通过总结它们合并这两个向量...任何帮助将不胜感激。简单地将它们绘制在一起是不够的,它们必须结合起来。

简单示例:

x1 = [1, 3, 5]
y1 = [1, 2, 3]
x2 = [6, 4, 2]
y2 = [1, 2, 3]

我想绘制y1 + y2,但我还没有找到任何方法。

1 个答案:

答案 0 :(得分:0)

来自https://www.mathworks.com/matlabcentral/newsreader/view_thread/37326

"您好, 当图的x值不是时,会出现问题 相同。你不必在求和之前插入图形。 这是一个小程序:

h=findobj(gca,'type','line'); % assuming there is only one figure exist or %the interesting figure is in focus
for n=1:length(h)
X{n}=get(h(n),'Xdata');
Xmin(n)=min(X{n});
Xmax(n)=max(X{n});
Y{n}=get(h(n),'Ydata');
end
xstart=max(Xmin); xstop=min(Xmax);
x=linspace(xstart,xstop,100); %you can put any other number of points
for n=1:length(h)
y(n,:)=interp1(X{n},Y{n},x);
end
ynew=sum(y,1);
hold on
plot(x,ynew,'k');

您可能需要调试代码。

乔 BSTEX - Matlab的方程式查看器"

相关问题