Skip certain values in plot and connect adjacent points

16 ビュー (過去 30 日間)
Daniyar Saparov
Daniyar Saparov 2019 年 8 月 27 日
編集済み: Adam Danz 2019 年 8 月 27 日
Hello,
I am trying to plot some data with a regular "plot" function, but I want to skip some certain values and connect the adjacent points of that skipped value. For example, I want to plot something like this:
x = 0:pi/10:2*pi;
y = sin(x);
plot(y)
and skip for examples values of x at points pi/2 or 3*pi/2, but connect the points around this skipped values. I tried:
x = 0:pi/10:2*pi;
y = sin(x);
plot(y(x~=pi/2))
but, it is not doing what I exactly want to do. I want the spacing of x values remain the same, except the skipped point, where the connected adjacent points obviously will have more separation than other pair of points. How can I do this kind of plot? Many thanks in advance.

採用された回答

Adam Danz
Adam Danz 2019 年 8 月 27 日
How to skip x values.
x = 0:pi/10:2*pi;
skipPoints = [pi/2, 3*pi/2]; % list points to skip
x(ismember(x,skipPoints)) = []; % remove skip-points
% now compute Y with only the desired points
y = sin(x);
plot(x,y)
If you'd rather not connect the points adjacent to the skipped values, you can replace the skipped values with NaN
x = 0:pi/10:2*pi;
skipPoints = [pi/2, 3*pi/2]; % list points to skip
x(ismember(x,skipPoints)) = NaN; % Replace with nan
% now compute Y with only the desired points
y = sin(x);
plot(x,y)
  2 件のコメント
Daniyar Saparov
Daniyar Saparov 2019 年 8 月 27 日
Hello Adam,
Many Thanks! The first one worked perfectly for me. Actually, I came up with another solution, assigning an average value of adjacent points to the skipped value, such as:
y(x==pi/2) = (y(x==pi/2-pi/10)+y(x==pi/2+pi/10))/2;
But your method is way better and saves a lot of space and time. Many thanks!
Adam Danz
Adam Danz 2019 年 8 月 27 日
編集済み: Adam Danz 2019 年 8 月 27 日
Glad I could help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by