Axis adjusting of an autocorrelation plot - matlab

I'm trying to create my autocorrelation function, because I don't have the Econometrics toolbox and I have succesfully created my function, but I can't get the x-axis in the stem-plot correct. The following picture will higlight my point:
As you can see in the picture there should be 4 where there is now 5. How can adjust the x-axis so that 5 will "jump" one step forward and so on...
Thank you for any help =)
EDIT:
Here is the code which generates this plot:
h = stem(corrVals, 'fill');
set(h,'MarkerFaceColor','red')
axis([1 25 -0.5 1.5])
Variable corrVals is a vector consisting of the autocorrelation values.
corrVals(1) is the autocorrelation at lag 0, corrVals(2) is the autocorrelation at lag 1 and so on...

Specify the x-values for stem:
h = stem(0:length(corrVals) - 1, corrVals, 'fill');

Related

How to resize the axes of an graph on Matlab?

I need ideas to resize my axes to have a much more airy graph to better visualize and calculate the gain between the different curves.
I used the code : axis([0 6 1e-3 1e0]) or xlim([0 6]); ylim([1e-3 1e0])
I would like to have for example my curve with: xlim([0:0.2:6]) (just the idea, otherwise it's wrong on matlab).
Thank you!
If I understand what you want, you need more XTicks in the x limits mentioned. After you plot just:
set(gca,'XTick',0:0.2:6)
another way is to write:
h=plot(.... whatever you plot...
h.XTick=0:0.2:6
Logarithmic Plot:
To create the axes the function xticks() and yticks() can be used to set the intervals, start and endpoints. xticks() and yticks() essentially take vectors that define all the ticks on the scales/axes. Just in case you'd like to also edit the interval along the y-axis. This vector can be created by raising each element in the vector (-3,1:0) to be an exponent with a base of 10. Finally, setting gca (the current axis) to logarithmic will allow the vertical spacing between the ticks to be evenly distributed.
axis([0 6 1e-3 1e0]);
Start = 0; Interval = 0.2; End = 6;
X_Vector = (Start: Interval: End);
xticks(X_Vector);
Y_Vector = 10.^(-3: 1: 0);
yticks(Y_Vector);
set(gca, 'YScale', 'log');
title("Logarithmic Plot");
grid;
Ran using MATLAB R2019b

How to make a confusion matrix plot with percentage values in Matlab

I would like to plot a 2x2 confusion matrix in Matlab in such a way that the plot would have percentage values in each of the grid boxes in this way:
In summary I'm asking for how to make a plot like the one above when you're given the percentage values in a matrix:
V = [0.15, 0.30; 0.05, 0.50]
Is it possible to make a plot like that in Matlab? I did think of drawing the vertical lines and then plotting some text into spesific coordinates, but is this the only way to do this?
Let me show you how you could do it. I did it just for fun!
You can change the size of your input matrix as you wish.
A=[0.3 0.2 ; 0.1 0.7];
sA=size(A);
Aplot=rot90(A,3);
figure;hold on
rectangle('Position',[0,0,sA(2),sA(1)],'Facecolor',[1 1 1],'edgecolor','none')
for ii=0:sA(1)
plot([0 sA(2)], [ii ii],'k','Linewidth',3)
end
for ii=0:sA(2)
plot([ii ii],[0 sA(1)],'k','Linewidth',3)
end
for ii=1:sA(2)
for jj=1:sA(1)
text((ii-1)+0.35,(jj-1)+0.5,strcat(num2str(Aplot(ii,jj)*100),' %'),'fontsize',30)
end
end
margin=0.05;
axis([0-margin sA(2)+margin 0-margin sA(1)+margin])
axis off
With Matlab 2014b and the new graphic engine, smoother option:

Histogram (hist) not starting (and ending) in zero

I'm using the Matlab function "hist" to estimate the probability density function of a realization of a random process I have.
I'm actually:
1) taking the histogram of h0
2) normalizing its area in order to get 1
3) plotting the normalized curve.
The problem is that, no matter how many bins I use, the histogram never start from 0 and never go back to 0 whereas I would really like that kind of behavior.
The code I use is the following:
Nbin = 36;
[n,x0] = hist(h0,Nbin);
edge = find(n~=0,1,'last');
Step = x0(edge)/Nbin;
Scale_factor = sum(Step*n);
PDF_h0 = n/Scale_factor;
hist(h0 ,Nbin) %plot the histogram
figure;
plot(a1,p_rice); %plot the theoretical curve in blue
hold on;
plot(x0, PDF_h0,'red'); %plot the normalized curve obtained from the histogram
And the plots I get are:
If your problem is that the plotted red curve does not go to zero: you can solve that adding initial and final points with y-axis value 0. It seems from your code that the x-axis separation is Step, so it would be:
plot([x0(1)-Step x0 x0(end)+Step], [0 PDF_h0 0], 'red')

Changing the length of x axis in MATLAB

I have the following code.
% Generate message signal
t1 = -1:0.1:1;
message_sig = rectpuls(t1);
L = length(message_sig);
figure(2)
stairs(0:L-1:L, 'linewidth',1.5);
axis([0 L -1 2]);
title('Message Signal');
When i run this, the length of my x axis is from 0 to 20.
How can i reduce it to say 0 to 8, while plotting the same bit pattern. Because when i try modulating and add noise, the whole plot (noise modulated signal) is blue and needs to be zoomed alot to see accurately.
So can someone help me with a code that could solve this.
simply use xlim([0,8]), that will restrict the x-axis from going beyond 8, or edit your axis call to be axis([0,8,-1,2])
UPDATE
Assuming you have the image processing toolbox, it is really simple
in = [0,1,0];
imresize(in,[1,8],'nearest');
This will take that pattern and expand it to whatever dimension you want.

Relative Frequency Histograms and Probability Density Functions

The function called DicePlot simulates rolling 10 dice 5000 times.
The function calculates the sum of values of the 10 dice of each roll, which will be a 1 ⇥ 5000 vector, and plot relative frequency histogram with edges of bins being selected in where each bin in the histogram represents a possible value of for the sum of the dice.
The mean and standard deviation of the 1 ⇥ 5000 sums of dice values will be computed, and the probability density function of normal distribution (with the mean and standard deviation computed) on top of the relative frequency histogram will be plotted.
Below is my code so far - What am I doing wrong? The graph shows up but not the extra red line on top? I looked at answers like this, and I don't think I'll be plotting anything like the Gaussian function.
% function[]= DicePlot()
for roll=1:5000
diceValues = randi(6,[1, 10]);
SumDice(roll) = sum(diceValues);
end
distr=zeros(1,6*10);
for i = 10:60
distr(i)=histc(SumDice,i);
end
bar(distr,1)
Y = normpdf(X)
xlabel('sum of dice values')
ylabel('relative frequency')
title(['NumDice = ',num2str(NumDice),' , NumRolls = ',num2str(NumRolls)]);
end
It is supposed to look like
But it looks like
The red line is not there because you aren't plotting it. Look at the documentation for normpdf. It computes the pdf, it doesn't plot it. So you problem is how do you add this line to the plot. The answer to that problem is to google "matlab hold on".
Here's some code to get you going in the right direction:
% Normalize your distribution
normalizedDist = distr/sum(distr);
bar(normalizedDist ,1);
hold on
% Setup your density function using the mean and std of your sample data
mu = mean(SumDice);
stdv = std(SumDice);
yy = normpdf(xx,mu,stdv);
xx = linspace(0,60);
% Plot pdf
h = plot(xx,yy,'r'); set(h,'linewidth',1.5);