Plot on the same figure in Matlab - matlab

I have the following code :
clc
clear
x=[1 2.5 2 3 5 6 3.5 2.1 4 .5]
y=[1 3 1.5 2 1.4 5 3.8 2.1 3 3.5]
p=plot(x,y,'r.')
set(p,'MarkerSize',30)
reg=polyfit(x,y,2)
p2=plot(reg)
how can I show both plot in the same figure and also I want axis range to be between 0 and 10.

I think this is what you really want to do:
x=[1 2.5 2 3 5 6 3.5 2.1 4 .5];
[x, inds] = sort(x);
y=[1 3 1.5 2 1.4 5 3.8 2.1 3 3.5];
y = y(inds);
p=plot(x,y,'r.');
set(p,'MarkerSize',30)
set(gca,'XLim',[0 10])
reg=polyfit(x,y,2);
hold on
plot(x, polyval(reg, x))

See http://www.mathworks.com/help/matlab/ref/hold.html
"hold on;"
hold on retains the current graph and adds another graph to it. MATLAB adjusts the axes limits, tick marks, and tick labels as necessary to display the full range of the added graph.

Related

Matlab nonlinear 2D figure plot

Let's say I have a set of data with x ranging from 0 to 5 (evenly distributed, say the spacing is 0.2), during this total x range, I actually have some fine features in terms of y values, say from 0 to 1 range. Beyond that, the features are coarse.
So now I want a figure to be like more zoomed into 0 to 1 range but still plotting out the total x range. The best scenario is of course not losing any data points.
Currently I do have one potential solution, where I pick up sparse points from the 1 to 5 range (spacing of 1 instead of 0.2) and plot out those data points evenly first. And then label with the correct corresponding x values as the following (those are just some random numbers I used here for explanation, the figure doesn't have fine features between 0 to 1 range though):
x=[0 0.2 0.4 0.6 0.8 1 2 3 4 5];
x1=0:9;
y=[0.1 0.2 0.3 0.4 0.5 1 2 3 4 5];
figure(1)
plot(x1,y,'-o')
set(gca,'xticklabel',x)
Figure plot
But this will obviously lose some information from the 1 to 5 range.
Is there a better way that I can still plot the whole range from 0 to 5 with the original data points but with a detailed showing of 0 to 1 range?
Thanks!
Not sure I understand. What about this?
x=[0:.2:1 2 3 4 5];
y=[0.1 0.2 0.3 0.4 0.5 1 2 3 4 5];
figure(1)
plot(x,y,'-o')
I would say the only proper way of having non-uniform intervals on the x-axis is by using a logarithmic scale. Try
semilogx(x,y,'-o')

How to compare any value in one matrix with any value in another matrix without using loops?

In the code below I have x any y coordinates of 4 nodes. First two nodes i.e. 1 and 2 fall in category c (matrix c) while the last two i.e 3 and 4 fall in category d (matrix d). "dist" represents distance between all the nodes. Is there any command in matlab using which i can tell if any of the nodes in c is at a distance less than R from any of the nodes in d e.g something like
if distance of any of the nodes in C from any of the nodes in D > R
%do this
end
I can do it using for loops but looking for a shorter way. Thanks
x=[1 2 4 4];
y=[3 5 6 1];
range=R
dist=[0.7 1.6 3.5 3.5; 2.9 0.7 1.6 4.7; 4.9 2.9 0.7 5.5; 3.8 4.3 4.5 0.7];
c=[1 2];
d=[3 4];
I do not completely understand your question, but from what I get the functions you are looking for are:
A>B - the binary operators in Matlab compares elements
any - returns true if any of the elements are true
pdist2(a,b) - compares distance from all observations in a vector a with all observations in another vector b.
I will gladly update with a more precise answer, but then please provide a complete example of what you want.

How to draw a Histogram in Matlab

I have a set of around 35000 data. These data are the signal strengths received only from a single location for different time interval of time. I want to plot a Histogram using these data. My X-axis will give the information about "Signal Strengths" and my Y-axis will give the information about "Probability". My histogram will consists of different bars which will give information about the signal strength and probabilities.
For example, suppose I have the following data
a= [ 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 5 6 6 6 6 6 6 6 6 6 6 6]
How can I plot the graph using data at X-axis and Probability at Y-axis? Any help will be appreciated. Thanks!
This should work just fine if you don't want to use some predefined functions:
una=unique(a);
normhist=hist(a,size(unique(a),2))/sum(hist(a));
figure, stairs(una,normhist)
Una has only the unique values of a, normhist is now between 0 and 1 and it's the probability of occurring of the individual signal because you divide it by the number of elements included in the data.

Matlab: Bar chart x-axis labels missing

I'm using the following code to create a bar chart in matlab.
a = [1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
bar(a)
set(gca,'XTickLabel',{'one','two','three','four','five','six','seven','eight','nine','zero','one','two','three','four','five','six','seven','eight','nine'})
Code works fine except that x-axis labels don't appear on their corresponding position on the x-axis. How do I resolve this issue?
When you set XTickLabel, you are telling Matlab to replace the text where each tick currently is with new text that you provide. If you run only the first two lines, you will see that Matlab by default has put XTicks on 0:2:20. You can resolve the issue by first telling Matlab to put ticks for each individual bar, and then re-labeling these ticks:
a = [1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
bar(a)
set(gca, 'XTick', 1:length(a))
set(gca,'XTickLabel',{'one','two','three','four','five','six','seven','eight','nine','zero','one','two','three','four','five','six','seven','eight','nine'})
You will have a pretty cluttered x-axis at this point...you would want to look into rotating the XTickLabels with the rotateXLabels function from FileExchange or with the built-in functionality on Matlab14b and later.

Draw lines between coordinates of a matrix in MATLAB

I would like to draw lines between the coordinates of the matrix below and then plot it, it should become an F.
F = ([0 2 2 7 7 2 2 8 8 0 0;0 0 8 8 10 10 14 14 16 16 0])
How can I do this?
You should simply be able to do this with
plot(F(1,:),F(2,:))
the style that you are plotting the data with has many options that you can refer to here http://www.mathworks.co.uk/help/matlab/ref/plot.html
Have you tried this?
plot(F(1,:),F(2,:),'k-.')
I dont have matlab here, but this must work!