Sum two plots and plot the result
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Is there a simple way to take the sum of two plots that overlap and plot the result? X and Y are the same length for both.

1 件のコメント
Rik
2019 年 9 月 7 日
The easiest way is to sum the data itself:
Z=X+Y;
plot(t,X,t,Y,t,Z)
採用された回答
Star Strider
2019 年 9 月 7 日
One approach (assuming that they are functions of the same independent variable vector, so in addition to being the same lengths, they are evaluated at the same values of the independent variable):
XplusY = sum([X(:),Y(:)],2);
This creates a column vector result, since we don’t know the sizes of the original ‘X’ and ‘Y’. If you want it as a row vector, transpose it.
To sum only the values where ‘X’ is greater than or equal to ‘Y’:
L = X >= Y;
XL = X(L);
YL = Y(L);
XplusY = sum([XL(:),YL(:)],2);
since I am not certain what ‘overlap’ means here.
9 件のコメント
Matthew Rotondaro
2019 年 9 月 7 日
This does not work since they are not functions of the same independent variable vector. Here, I have in one plot a set of X and Y values, and I have in the second plot X shifted by some amount and Y scaled by some amount. So the vectors remain the same lengths, but possess different values.
Rik
2019 年 9 月 7 日
Then you will have to interpolate one or both to a shared x, after which you will be able to add them up.
Star Strider
2019 年 9 月 7 日
If they don’t have a common independent variable, you can’t sum them and get a meaningful result.
You will have to create a common independent variable for both, then use interp1 to interpolate both of them to it. Then you could sum the interpolated curves.
Example —
Xi = linspace(1.2, 3.4, 50);
X = exp(-((Xi-2.1)/0.5).^2);
Yi = linspace(2.3, 3.4, 50);
Y = exp(-((Yi-2.9)/0.2).^2);
figure
plot(Xi, X, Yi, Y)
grid
XiYi = [Xi(:); Yi(:)];
Ci = linspace(min(XiYi), max(XiYi), 150);
Xq = interp1(Xi(:), X(:), Ci(:), 'linear','extrap');
Yq = interp1(Yi(:), Y(:), Ci(:), 'linear','extrap');
Sq = sum([Xq(:) Yq(:)],2);
figure
plot(Ci, Xq, Ci, Yq, Ci, Sq)
grid
Experiment to get the result you want.
Matthew Rotondaro
2019 年 9 月 7 日
That works! Thank you!
Star Strider
2019 年 9 月 8 日
As always, my pleasure!
Susmita Panda
2021 年 5 月 24 日
But the in the summation plot, the second also starts from starting rather it should have a delay?
Niko
2023 年 7 月 5 日
What if they are both independent, i.e. same y and x "measurements", but with different density points (x-axis is more denser on one with higher resolution in a smaller window than the other)?
Star Strider
2023 年 7 月 5 日
With respect to different independent variable ranges, the interpolation is still necessary, however they can only be summed in the ranges where all the curves exist. All the curves would need to be truncated to those limits, then the interpolated values summed, ignoring regions where they do not all overlap.
With respect to different resulutions, it is preferable to interpolate the higher resolution signal to match the values in the lower resolution signal. This avoids the problem of creating data in the lower resolution isgnal where none previously existed, as would be the problem if the lower resolution signal were interpolated to match the higher resolution signal, since the exact behaviour of the lower resolution signal at those points is obviously not known.
.
Rik
2023 年 7 月 6 日
You might consider performing a moving average before downsampling. Otherwise you may introduce other effects (consider the situation where one of the curves is a sine and the resolution of the other curve is equal to the period of the sine).
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Surface and Mesh Plots についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
