I am new to Matlab and I think this is a very basic question..
I have two arrays:
tst
ans =
0 0 0.2500 0.2500 0 0 0 0
prp
ans =
0 0 0 0.5333 0.0333 0.0333 0.1667 0.0667
I want to plot the averages between those two on the same plot (as a line). What is the best way to do it?
I usually do something like:
# Plot figure
plot(...);
# Calculate the average
avg = mean(tst);
# Add a line to the figure
line(xlim(), [avg avg]);
And repeat for prp. You can style the lines however you want according to the linespec documentation.
Note that you should do this at the end of anything that would change the xlimits (holding and plotting something else, etc.) as it depends on xlim().
Edit
I may have misunderstood your question. The above code will plot a horizontal line at the average of the points in the array tst.
If instead you want a line plot whose points are the average of the two arrays, you want something like:
# Calculate average between arrays
avg = mean([tst;prp]);
# Plot all 3 lines at once
plot([tst;prp;avg]');
Related
I have a number vector, let's say v=[1 1 1 1000 20 20]; I want to build very simple histogram-like plot where y-axis will be counts ( 3,1,2 in this case), but the x-axis will be 1,20,1000 equally spaced. Meaning that Matlab will ignore the fact that 1000>>20 and space them as it will 1,2,3 in the plot.
I will show that I mean. I can do it by:
x=[1 1 1 1000 20 20];
histogram('Categories',{'1','20','1000'},'BinCounts',[numel(find(x==1)), numel(find(x==20)), numel(find(x==1000))])
But it's kind of clumsy and gawky way. I have to know the categories beforehand. Can it be done in more elegant way?
first convert X to categorical matrix
convert to categorical
then histogramplot X with these categories:
plot categorial histogram
in case of your problem:
C = categorical(x,[1 20 1000],{'1','20','1000'})
//or just C=categorical(x,[1 20 1000]) or simply C=categorical(x)
histogram(C)
I would like to reproduce the following plot in MATLAB:
For instance, consider the following time series:
a= [1,0.5,0.25, -0.5, -0.75,0.5,1.25, -0.8,0.1,0.2,0,3,0.8, -0.9, -1,1]
How can I plot values above 0 in one color and values below another color?
The trick is to avoid the plotting of points by replacing their values by NaN.
I suggest to first separate a in two arrays, lets say a_lo and a_hi.
a_lo is a but with positive values replaced by NaN.
a_hi is a but with negative values replaced by Nan.
So you will do something like this:
a_lo = a;
a_hi = a;
for i = 1:length(a)
if a_lo(i) > 0
a_lo(i) = NaN;
end
if a_hi(i) < 0
a_hi(i) = NaN;
end
end
Then you plot a_lo and a_hi with different colours, don't forget hold on to plot the two curves together.
plot(a_lo,'r'); hold on;
plot(a_hi,'b');
Here is an example of what I could obtain with a sine wave:
I have something like this:
t = [-1 0 1 2 3 4 5];
ft= [ 0 0 0 0 1 1 1];
I want to plot only horizontal levels high\low without vertical lines:
If you don't mind the vertical lines, it's really simple to just use the stairs(x,t) function. Otherwise you can create your own function that deals with pairs of points to generate lines and plot them all individually using hold on.
function stairs2(x,y)
hold on;
for i=1:length(x)-1
plot(x(i:i+1),[y(i) y(i)]);
end
hold off;
end
Then just call stairs2(x,t) as per your example above, and set appropriate zoom/axes.
Alternately, this is a different way that only uses ONE call to plot:
function stairs2(x,y)
for i=1:length(x)-1
A(:,i) = [x(i) x(i+1)];
B(:,i) = [y(i) y(i)];
end
plot(A,B,'b');
end
I'm trying to do an algorithm in Matlab to try to calculate a received power in dBm of a logarithmic model of a wireless telecommunication system..
My algorithm calculate the received power for a number of distances in km that the user specified in the input and stores it in a vector
vector_distances = { 1, 5, 10, 50, 75 }
vector_Prx = { 131.5266 145.5060 151.5266 165.5060 169.0278 }
The thing is that I almost have everything that I need, but for graphics purposes I need to plot a graph in where on the x axys I have my vector of receiver power but on the y axys I want to show the same received power but with the most complete logarithmic model (the one that have also the noise - with Log-normal distribution on the formula - but for this thing in particular for every distance in my vector I need to choose 50 numbers with 0.5 distance between them (like a matrix) and then for every new point in the same distance calculate the logarithmic model to later plot in the same graph the two functions, one with the model with no noise (a straight line) and one with the noise.. like this picture
!http://imgur.com/gLSrKor
My question is, is there a way to choose 50 numbers with 0.5 distance between them for an existing number?
I know for example, if you have a vector
EDU>> m = zeros(1,5)
m =
0 0 0 0 0
EDU>> v = 5 %this is the starter distance%
v =
5
EDU>> m(1) = 5
m =
5 0 0 0 0
% I want to create a vector with 5 numbers with 0.5 distance between them %
EDU>> for i=2:5
m(i) = m(i-1) + 0.5
end
EDU>> m
m =
5.0000 5.5000 6.0000 6.5000 7.0000
But I have two problems, the firs one is, could this be more simplex? I am new on Matlab..and the other one, could I create a vector like this (with the initial number in the center)
EDU>> m
m =
4.0000 4.5000 **5.0000** 5.5000 6.0000
Sorry for my english, and thank you so much for helping me
In MATLAB, if you want to create a vector from a number n to a number m, you use the format
A = 5:10;
% A = [5,6,7,8,9,10]
You can also specify the step of the vector by including a third argument between the other two, like so:
A = 5:0.5:10;
% A = [5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10]
You can also use this to count backwards:
A = 10:-1:5
% A = [10,9,8,7,6,5]
I have a vector for which I need to calculate a threshold to convert it to a binary vector (above threshold=1, below=0). The values of the vector are either close to zero or far from it. So if plotted the vector, values either lie near X-axis or shoot up high(so there is a clear difference between the values). Each time, the values in the vector change so I need to calculate the threshold dynamically. There is no limit on max or min values that the vector can take. I know that otsu's method is used for grayscale images but since the range values for my vector is varying, I think I cannot use it. Is there any standard way to calculate threshold for my case? If not, are there any good workarounds?
I suggest you specify the percentage of values that will become 1, and use the corresponding percentile value as the threshold (computed with prctile function from the Statistics Toolbox):
x = [3 45 0.1 0.4 10 5 6 1.2];
p = 70; %// percent of values that should become 1
threshold = prctile(x,p);
x_quant = x>=threshold;
This approach makes the threshold automatically adapt to your values. Since your data are unbounded, using percentiles may be better than using averages, because with the average a single large value can deviate your threshold more than desired.
In the example,
x_quant =
0 1 0 0 1 0 0 0
if the limits dont differ in a single vector and the 0 and 1 values are nearly equal in probability, why dont you simply use the mean of the vector as a threshold?
>> X=[6 .5 .9 3 .4 .6 7]
X =
6.0000 0.5000 0.9000 3.0000 0.4000 0.6000 7.0000
>> X>=mean(X)
ans =
1 0 0 1 0 0 1
if the probability is different for ones and zeros you might want to multiply the mean in the comparison to fit again. note that this is a very simplistic aproach, which can surly be improved to better fit your problem