How to plot histograms of two different classes in a single plot? - matlab

I have two draw histogram of two clases in to single plot. One class is of distance of authenticated users and others if of distance of intruduers achieved by L2-NORM.
intruder_dist =[0.02, 0.05,0.03......] and another is its corresponding number. intruder_num = [10,2,40,..........]
Same is with authencticated users distances.
intruder_dist =[0,0.002,0.001......] and another is its corresponding number. intruder_num = [30,50,70,..........]
Below is the histogram that I want to achieve. How to plot it in matlab.

If I understand correctly, you just need hold on between the two calls to histogram/ hist. Here's an example:
histogram(10+2*randn(1,1e4), 21)
hold on
histogram(2+.3*randn(1,1e4), 11)

Related

Matlab - how does contour plot generating levels automatically?

I am using contourf to generate filled contour plots on MatLab with specified levels number.
According to the documents (https://www.mathworks.com/help/matlab/ref/contourf.html#mw_9088c636-4036-4e00-bd43-f6c5632b63ec)
It says Specify levels as a scalar value n to display the contour lines at n automatically chosen levels (heights).
I am wondering how does it choose the threshold automatically? What is the algorithm of choosing the thresholds? Take level as 1 as an example.
Many thanks!
As said in the comments, it just makes sure there are n dividing lines between your max and min.
Proof:
n=10;
z=peaks;
[m,c]=contour(z,10,'ShowText','on');
levels=linspace(min(z(:)),max(z(:)),n+2);
isequal(c.LevelList,levels(2:end-1))

How do I correctly plot the clusters produced from a cluster analysis in matlab?

I want to carry out hierarchical clustering in Matlab and plot the clusters on a scatterplot. I have used the evalclusters function to first investigate what a 'good' number of clusters would be using different criteria values eg Silhouette, CalinskiHarabasz. Here is the code I used for the evaluation (x is my data with 200 observations and 10 variables):
E = evalclusters(x,'linkage','CalinskiHarabasz','KList',[1:10])
%store kmean optimal clusters
optk=E.OptimalK;
%save the outouts to a structure
clust_struc(1).Optimalk=optk;
clust_struc(1).method={'CalinskiHarabasz'}
I then used code similar to what I have found online:
gscatter(x(:,1),x(:,2),E.OptimalY,'rbgckmr','xod*s.p')
%OptimalY is a vector 200 long with the cluster numbers
and this is what I get:
My question may be silly, but I don't understand why I am only using the first two columns of data to produce the scatter plot? I realise that the clusters themselves are being incorporated through the use of the Optimal Y, but should I not be using all of the data in x?
Each row in x is an observation with properties in size(x,2) dimensions. All this dimensions are used for clustering x rows.
However, when plotting the clusters, we cannot plot more than 2-3 dimensions so we try to represent each element with its key properties. I'm not sure that x(:,1),x(:,2) are the best option, but you have to choose 2 for a 2-D plot.
Usually you would have some property of interest that you want to plot. Have a look at the example in MATLAB doc: the fisheriris data has 4 different variables - the length and width measurements from the sepals and petals of three species of iris flowers. It is up to you to decide which you want to plot against each other (in the example they choosed Petal Length and Petal Width).
Here is a comparison between taking Petals measurements and Sepals measurements as the axis for plotting the grouping:

Interpolating 3D points from input points corresponding to a closed surface

I have a list of scattered 3D points similar to the one below:
Using MATLAB, I want to interpolate further points from the surface that those original points correspond to, in order to obtain a more complete scatter. Note that there are no particular slices defined on this scattered data. That is, the z values of the point cloud are not discrete, so it's not possible to interpolate slice by slice.
I think that the ideal way to achieve this would be to somehow obtain the smooth closed surface which best matches the scattered data, and then sample it. But I have found no straightforward way to achieve this.
The scatterinterpolant class could be a simple option.
Use scatteredInterpolant to perform interpolation on a 2-D or 3-D
Scattered Data set. For example, you can pass a set of (x,y) points
and values, v, to scatteredInterpolant, and it returns a surface of
the form v = F(x, y). This surface always passes through the sample
values at the point locations. You can evaluate this surface at any
query point, (xq,yq), to produce an interpolated value, vq.
http://au.mathworks.com/help/matlab/math/interpolating-scattered-data.html
Scattered data consists of a set of points X and corresponding values
V, where the points have no structure or order between their relative
locations. There are various approaches to interpolating scattered
data. One widely used approach uses a Delaunay triangulation of the
points.

Using Matlab's histogram() to simultaneously plot two sets of data

I have an matrix A containing two columns for two sets of data. I want to plot these on the same histogram, using different colors for the bars representing the class intervals. I can do this using hist(A), in which case each column in the matrix is taken as a distinct set of data. I would like to use histogram() instead, and also define edges for bins. How can I do this?
The answer is simpler than I thought:
histogram(A(:,1))
hold on
histogram(A(:,2))
This automatically makes the bars transparent, so both can be seen.

How to plot a probability density distribution graph in MATLAB?

I have about 10000 floating point data, and have read them into a single row matrix.
Now I would like to plot them and show their distribution, would there be some simple functions to do that?
plot() actually plots value with respect to data number...which is not what I want
bar() is similar to what I want, but actually I would like to lower the sample rate and merge neighbor bars which are close enough (e.g. one bar for 0.50-0.55, and one bar for 0.55-0.60, etc) instead of having one single bar for every single data sample.
would there be a function to calculate this distribution by dividing the range into small steps, and outputting the prob density in each step?
Thank you!
hist() would be best. It plots a histogram, with a lot of options which you can see by doc hist, or by checking the Matlab website. Options include a specified number of bins, or a range of bins. This will plot a histogram of 1000 normally random points, with 50 bins.
hist(randn(1000,1),50)