I want to make a plot that discontinues at one point using Matlab.
This is what the plot looks like using scatter:
However, I would like to the plot to be a smooth curve but not scattered dots. If I use plot, it would give me:
I don't want the vertical line.
I think I can break the function manually into two pieces, and draw them separately on one figure, but the problem is that I don't know where the breaking point is before hand.
Is there a good solution to this? Thanks.
To find the jump in the data, you can search for the place where the derivative of the function is the largest:
[~,ind] = max(diff(y));
One way to plot the function would be to set that point to NaN and plotting the function as usual:
y(ind) = NaN;
plot(x,y);
This comes with the disadvantage of losing a data point. To avoid this, you could add a data point with value NaN in the middle:
xn = [x(1:ind), mean([x(ind),x(ind+1)]), x(ind+1:end)];
yn = [y(1:ind), NaN, y(ind+1:end)];
plot(xn,yn);
Another solution would be to split the vectors for the plot:
plot(x(1:ind),y(1:ind),'-b', x(ind+1:end),y(ind+1:end),'-b')
All ways so far just handle one jump. To handle an arbitrary number of jumps in the function, one would need some knowledge how large those jumps will be or how many jumps there are. The solution would be similar though.
you should iterate through your data and find the index where there is largest distance between two consecutive points. Break your array from that index in two separate arrays and plot them separately.
Related
I have a 100 x 100 matrix and i have to use plot3 in MATLAB environment to graph this data. I tried plot3(matrix name) but I faced this error "not enough input arguments". I think plot3 needs 3 input arguments, but I only have this matrix of data. could anyone help me to solve this problem? Is there any alternative for plot3 when we don't have enough arguments?
I need a graph like this:
I think you want to plot the values in a figure as a sort of surface element. What you can do then is:
[X,Y] = size(matrix);
figure;
surface(1:X,1:Y,matrix);
What this does is that it creates a vector for both X and Y indices, as possible in surface. The X and Y indices are obtained by setting them as integers from 1:size, so basically you assign the location of each matrix element to an index.
Note that you can strictly speaking use surface(matrix) as well, but the former approach allows you to use custom indexing, as long as the lengths of the vectors X and Y are the same as the size of your matrix.
For the waterfall use:
figure;
waterfall(matrix);
Sample code:
A=rand(100);
figure;
waterfall(1:100,1:100,A);
Gives:
where you can play around with the name-value pairs, see the documentation on that.
I think what you need is mesh or surf instead of plot3.
plot3 draws a line in 3d-space, so it will need three vectors of the same length (one for each dimension).
When you have a matrix, one reasonable way of displaying it is as a surface in 3d space, which is done by the functions mesh and surf.
Try it out! I hope i helps!
I have a matrix which is 1*1*10000, the slightly odd dimensions are the result of the matrix algebra used to calculate it.
I simply want to be able to plot the 10000 data points contained in it, but matlab seems unable to do it?
Can someone please tell me how I can plot the data?
Seems simple but I really can't figure out how to do it!
Baz
yes you need to reduce the dimensions to a vector:
A = zeros(1,1,100)
vector = squeeze(A(1,1,:))
as when you'd access the third dimension this would only return a 3D-Matrix again:
z = A(1,1,:)
would NOT work. So use squeeze() ;-) Then plot as usual.
Doc-Link: http://www.mathworks.de/de/help/matlab/ref/squeeze.html
And as Ander pointed out in comments, no need to give any dimensions, as it removes singleton-dimensions by itself. So just use vector = squeeze(A). MATLAB recognizes the way to go itself.
I am trying to plot 3 vectors onto matlab GUI in a serial object's callback.
I want to plot this on axes handle but the problem is it only plot last vector;
plot(handles.axes1,sensor1,'r');
plot(handles.axes1,sensor2,'b');
plot(handles.axes1,sensor3,'g');
I searched on internet and find that this issue can be solved with hold on and hold of feature so I tried this
plot(handles.axes1,sensor1,'r');
hold on ;
plot(handles.axes1,sensor2,'b');
plot(handles.axes1,sensor3,'g');
hold off;
but in this case a new figure is opened(dont know why) and again only the last plot is drawn.
I am stucked. If any one have idea of what would be the issue?
Thanks
I'm not sure why your first try using "hold" didn't work. Seems like it should have.
But in any case, you can get the desired behavior in a single command:
plot(handles.axes1,length(sensor1),sensor1,'r',...
length(sensor2),sensor2,'b',...
length(sensor3),sensor3,'g');
This specifies both an X = length(sensor_) and a Y = sensor_ to the plot command. When you only give plot a Y input, it assumes an X of length(Y). But you can't combine multiple traces in a single plot command by giving only the Y input for each, because it will try to treat the inputs as X,Y pairs.
As the vectors are the same length we can simply combine them as the columns of a matrix and then plot the matrix
plot(handles.axes1,[sensor1',sensor2',sensor3'])
However these will have the default colour order. Without specifying x values setting colors within the plot command is tricky. However (luckily) the default order starts:
blue,green,red...
so swapping the column order will plot the lines with the colours requested
plot(handles.axes1,[sensor2',sensor3',sensor1'])
(this assumes the vectors are rows, if they are columns don't transpose them)
I have some 3D parametric equations that I plot in matlab/octave and would like to find out where they intersect how can I go about doing this. Please note this is just a simple example I plan on having multiple parametric equations that will intersect.
What I'm trying to do is begin and end each plot at an intersection point.
My first thought was putting all the intersection points found at (t) into an array and plotting the ones I want I just couldn't figure out how to get the intersections at (t)
Example matlab/octave code below:
clear all, clf
t = 0:pi/60:2.45*pi;
plot3 ((t).*cos(t), (t).*sin(t), (t),'b*');
hold on
plot3 ((t).*sin((t)), (t).*cos((t)), (t),'r');
Here's an image with the arrows pointing to the intersecting points
I'd say that it is better to solve this system of equations analytically if possible.
Look, with your original code (just the plotted line styles were changed)
clear all, clf
t = 0:pi/60:2.45*pi;
plot3 ((t).*cos(t), (t).*sin(t), (t),'*b-');
hold on
plot3 ((t).*sin((t)), (t).*cos((t)), (t),'or-');
I have this picture at one of the intersections:
Its nice because the two plots intersect perfectly with each other in one of the plotted points.
Now consider the following situation. I changed your second line to
t = 0:0.05:2.45*pi;
Now I see this at the intersection point:
Now the two 3D polygonal lines generated for different values of t parameter in general case will not have any common points. So simple check for intersection of those linear segments will fail.
Same thing will happed if you update your equations to more complicated ones with more complicated dependencies over t (otherwise you are really lucky).
Analytical solution (if possible) will be unbeatable here.
As a numerical way to do this, consider distance D from an arbitrary point P1(x1(t1),y1(t1),z1(t1)) on one line to an arbitrary point P2(x2(t2),y2(t2),z2(t2)) on another one. Then find minimum of D as a function of two independent parameters t1 and t2. If D at minimum is zero (or very close to zero), then these two lines intersect. Otherwise they just pass each other.
I have a 1 x 10 vector as shown below:
V = [0.500 -5.433 0.543 0.321 1.432 0.543 -0.576 -0.145 -1.322 -0.222]
and want to plot this on MATLAB using plot.
I used plot(v,0,'kx,'marker',10) but it does not seem reasonable for me. Any idea on how to go about with this?
Does anyone have a very good resource for for ISOMAP? Need a very comprehensive step by step easy tutorials on Isomaps. If i can have good videos on it will be very good.
you almost got it, just write:
plot(v,'kx','MarkerSize',10);
note, that I wrote plot and not Plot, Matlab is case sensitive...
when you only have a single vector the plot function assumes that for the x-axis it takes the number of elements of the vector, i.e. plot(1:numel(v),v,...). I recommend you to use the Matlab documentation, if you had read it, you'd see an example that could show you what you did wrong.
bla's solution is okay and produces a scatter plot.
However, you can make a line plot, etc.
Though, notice it is V and not v, as he pointed out that MATLAB is case-sensitive.
Here is how to generate a line plot:
x=0:length(V)
plot(x,V,'r--o',x,V,'r*')
Output: