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

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.

Related

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

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)

Fitting gaussians to close peaks in MATLAB

I have a data set that has two peaks close together. I'd like to fit these peaks with gaussians so that I come up with a new data set that replicates the original one. To this end, I am using MATLAB's "findpeaks" function, and using the heights and widths of the peaks in order to come up with the appropriate number of gaussians, and then add those gaussians together. However, because the peaks are so close together, the result looks like the following (with the original data set in blue and the replicated one in red):
Is there a better method to replicate the data with gaussian peaks?
Gaussian function are defined by two variable, mean and variance. The two peak would give you the means of the two gaussians and by the look of the figure the same variance for them both (If some data has gone through a Gaussian process the variance would be the same, I can not think of a physical process where that would not be the case, unless it is just an arbitrary plot). So you only have to find one variable. As for the peaks that would just be the normalization so that the area under the curve sums up to 1. A gaussian sums up to 1 by default, if the sum under the plot you are trying to fit is 2 you do not need to do anything, otherwise scale accordingly.
My guess is something like this (pseudo code):
f = 0.5*gauss(-3,var)+0.5*gauss(3,var)
If you know more about the process that created the plot, then you can actually do better.

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)

Save the values of Surface function in MATLAB

I use the surf function in MATLAB in order to create a 3D map.
This map is created by 2 vectors and for each combination of their values I have a third one that creates the surface.
Is there a way to save for each iteration these sets of three values somehow?
What I want to achieve is to have a matrix which will be as a map for every possible combination of these values.
I've made some efforts with 3D matrices but I made it even more complex and I had many errors because of the dimensions.

MATLAB: plotting multiple columns of a matrix

Inside a MATLAB function I have built a matrix A, whose dimensions M and N are set as parameters of the function. I would like to plot all the columns of this matrix, given a vector of indices B with length M. Hence, I use these lines:
figure
plot(B,A)
I specified figure as the MATLAB function returns more different plots.
My problem is that the program plots just two columns of the matrix with different colours (blue and violet). Where is my mistake?
Thank you for your attention.
go for
plot(repmat(B,1,N),A);
or
plot(repmat(B,N,1),A);
(depending on your rows/columns). You need to have same size matrices in plot.
Moreover, if B are just consecutive indexes, you may want to consider Plot(A) (or Plot(A')).
I noticed that there was an error which caused the overlap of the different curves, so the way which I used to plot the colums of a matrix is valid. However, the method proposed by Acorbe is a possibility, too.