plot piecewise function in matlab - probability

This is my code:
p=.2;
x=-1:20;
px=p*(1-p).^(x-1).*(x>=1);
Fx=cumsum(px);
figure;
subplot(2,1,1);
stem(x,px);
subplot(2,1,2);
stairs(x,Fx);
hold on;
stem(x,Fx,'w.')
I want to draw a circle mark at the right end of each horizontal line on the second subplot. Could someone show me how to do? Thanks in advance~
Things like following figure, but move the circle to the right end of each line.

You can use plot or scatter to draw the red circles.
Add one of the following instructions at the end of your script, they give the same output.
% Option 1: using plot
plot(x+1,Fx,'or');
% Option 2: using scatter
scatter(x+1,Fx,'r');
As you can see, my approach was to simply add 1 to every element of x. This is essentially shifting your data 1 unit in the x direction.
This is what you get:

Related

Hold an imagesc on matlab gui

I have a little mess with a GUI developed on Matlab. Basically, I have two axes, one to plot a bitmap I want to keep all the time and another one to do some ordinary plots. My problem is that when I try to do any plot on the second axes, the image on the first one disappears and the axis get restarted. I tried to plot over the image and it remains on the figure (only shifts because the axis change once the previous image is gone).
Any idea aout what is happening? By the way, it is for a chromatic tuner that plots a degradation from green to red depending on the accuracy and the FFT of the received signal.
1) Here is the code for the image plot:
colormap(cmap);
imagesc(bmp,'Parent',handles.axes_tuner);
hold(handles.axes_tuner,'on');
% Vertical line in front of the image
plot([L/2,L/2],[-length(bmp(:,1)),2*length(bmp(:,1))],'b','LineWidth',1.5);
axis(handles.axes_tuner,'off');
hold(handles.axes_tuner,'off');
2) Plot on the other axes
cla(handles.axes_fft);
hold(handles.axes_fft,'on');
plot(f,spectrum,'b-','Parent',handles.axes_fft);
xlabel(handles.axes_fft,'Frequency','FontSize',8);
axis([0 Fs -50 0]);
ylabel(handles.axes_fft,'Normalized Amplitude','FontSize',8);
hold(handles.axes_fft,'off');

Timing issue on multi-line chart?

I am working on a multi-line animated chart based on [Edit: not Bostock's] example:
http://bl.ocks.org/atmccann/8966400
I want the lines to appear synchronized by the date value on the x axis. If I slow down the graph (say to duration=15000) I can clearly see that in the very early part of the graph the green line is ahead of the black line. Then the black line moves ahead of the green line. Both start and end together.
The issue is accentuated in my chart where I am plotting 20 lines. Some are clearly ahead of the others when plotting. Lines with initial y values of zero shoot way ahead of those that climb to higher y values at the start.
How can I adjust the display so both lines paint for the same date at the same time?
The technique in the bl.ocks.org example (which, btw, is not Bostock's) is animating based on the length of the line, not it's position on the y-axis. If you want to animate based on the y-axis position, you'll have to use a different technique. Especially with 20 lines, I wouldn't recommend trying to dynamically update each line if they have a lot of points. You could do that if there aren't many points, though.
A simple approach would be to add a solid (white) rectangle to the chart that covers all the graph lines. Then animate the left position and width of that rectangle to reveal the lines over time.

How to create a flexible rectangular box with perpendicular height and width line at the center to measure eight and width in matlab

I am wondering if there is a tool to create a flexible box with (+ line )height and width line to measure the object height and width in an image in matlab.I am right now using imline but have to draw the 2 lines perpendicular which sometime goes wrong, hence wondring if there is a tool which I can use.
Thanks,
If you have the IP toolbox, a convenient function for this is imrect
Here is a working example:
im=imread('http://i.imgur.com/Y4106D4.png');
imshow(im);
h = imrect;
pause
pos = getPosition(h);

How can i draw a slanted rectangle in a frame (or picture)?

I have a code that takes a video, split it into frames and then asks me how many rectangles would i like to draw on the frame (only the first frame, kin the code below).
I would like to be able to draw a slanted rectangles too. Is there any command that can help me?
if k==1
result=input('How many rectangles would you like to draw? ');
pos=zeros(result,4);
for i=1:result
handle=imrect;
pos(i,:)=handle.getPosition;
end
end
In your code, imrect is responsible for drawing a rectangle.
You may use impoly to draw some polygons (here slanted rectangles).
The code
imshow('cameraman.tif')
h_poly = impoly;
will give
You can modify the shape by drag/dropping the selection's blue handles.
The positions of the blue handles can be known with
pos = getPosition(h_poly)

jqPlot Cursor follow series line

I have a line graph using jqPlot with one series and several data points across it and smoothed lines. I'm using the Cursor plugin to show crosshairs and a tooltip to show x and y points.
Is it possible to have the cross hairs follow the line on the series? So the horizontal line would fix to the y position of the line and not following the mouse. I see you can get the x/y position of each data point but not of the lines inbetween points.
Thanks
If you are using the built in smoothing options the smoothed data points are stored at:
plotObj.series[0].renderer._smoothedPlotData
with pixel locations at:
plotObj.series[0].renderer._smoothedData

Resources