Connecting the point selectively

  • Thread starter Thread starter hokhani
  • Start date Start date
  • Tags Tags
    Point
AI Thread Summary
The discussion focuses on a MATLAB code snippet that generates two discrete regions based on the sine function. The user aims to connect points within each region while keeping them visually separated. The solution involves breaking the data into two halves and plotting them separately, which prevents the rightmost point of the left region from connecting to the leftmost point of the right region. The modified code uses the '-*' option in the plot function to connect points with lines while ensuring they are displayed in different colors. To maintain a consistent color for both regions, users can specify a color code, such as '-*b' for blue. This approach effectively visualizes the two regions without merging them.
hokhani
Messages
561
Reaction score
18
Running the Matlab code below two discrete regions appear; one in left and the other in right.
Code:
x=20:5:800;
xpr=x*pi/180;
y=sin(xpr);
n=find(y<.2);
plot(x(n),y(n),'*')
I would like to connect the points of each region together and not to connect the rightmost point of the left region to the leftmost point of the right region so that the two regions remain separated. What should I do? I appreciate any help.
 
Physics news on Phys.org
Code:
x=20:5:800;
xpr=x*pi/180;
y=sin(xpr);
n=find(y<.2);

X = xpr(n);
Y = y(n);
N = length(X)/2;
plot(X(1:N), Y(1:N), '-*', X(N+1:end), Y(N+1:end), '-*')

Since you scaled x to xpr, I plotted xpr since it is used to calculate y. But essentially all I did was break X and Y in half, then plot the first and second halves separately in the call to plot. The '-*' ensures that plot draws the point and connects with a line. The result is below. Note that since the plots are separate, they get different colors. You can get around that by specifying the same color for both, for example by saying '-*b' to make both plots blue.

sin_plots.png
 
  • Like
Likes hokhani
Back
Top