Is there anyway to find the density of histogram values in MATLAB? - matlab

I have 7 vectors ranged between 0 and 0.99. The number of entries in each vector is different, so it would be "unfair" to compare their histograms because there should be a direct correlation between a bin count and the number of entries, assuming the variables are spaced. I'm interested in plotting a smooth curve of the density of the values. So, for a vector a with say n values from 0 and 0.99, I would like an x-axis of 0 to 0.99, with the y-axis being the probabilities associated with those values.
Any ideas or insight?

Not sure what kind of smoothing you want, but some idea how to start:
%some example vectors of different length
p=[10,100,1000,10000];
D=arrayfun(#(x)(rand(x,1)),p,'uni',false);
%defining the range
support=[0:.1:1];
%make sure we don't miss a value
esupport=support;
esupport(end+1)=inf;
%define a function which uses histc to calculate the emperical probability for each bin
epdf_bin=#(x)histc(x,esupport)/numel(x);
%evaluate emperical probability
E=cellfun(epdf_bin,D,'uni',false);
M=cat(2,E{:});
%plot
bar(M);
%print legend
legend(arrayfun(#num2str,p,'uni',false));
%fix x axis labels
set(gca,'XTick',.5:numel(support))
set(gca,'XTickLabel',support)

[h,b] = hist( my_data, Nbins );
plot( b, h / sum(h) );

Related

How to make normalized frequency distribution plot for 2D matrices?

I have two 80*80 matrices. I would like to plot normalized frequency plot. I tried to normalize the 80* 80 grid with the following code:
A = per_monsoon_postmon; % (A is my 80*80 matrix)
A = rand (80,80);
minA = repmat(min(A), [size(A, 1), 1]);
normA = max(A) - min(A);
normA = repmat(normA, [length(normA) 1]);
normalizedA = (A - minA)./normA;
But this code didn't give me the desired result, as grids with nan values also has a number in it. For eg. earlier grid 1*1 is nan now it has a value of 0.8340. Could you please help me how to normalize the 2D matrix and then plot frequency distribution plot in MATLAB? Is there a way to directly plot normalized frequency distribution plot?
If you have nan values in your verctor you might have problems. I would first replace the nans (for example with zeros).
Normalisation between 0 and one works like this:
a=rand(80,80); %generates random 80x80 array
a=a-min(min(a)); %shifts the values from 0 to n, min(min() ) because it is 2x2
a=a./max(max(a));% shifts to 0 to 1
If you want tot plot these values in 3d I would use a surf plot therefore you first generate the sample values and then feed them the z values
[x,y]=meshgrid(1:80);
surf(x,y,z)

matlab scatter plot using colorbar for 2 vectors

I have a two columns of data. X = Model values of NOx concentrations and Y = Observations of NOx concentrations. Now, I want to scatter plot X, Y (markers varying with colors) as well as the colourbar which would show me the counts (i.e. number of data points in that range). X and Y are daily data for a year, i.e. 365 rows.
Please help me. Any help is greatly appreciated.
I have attached a sample image.
If I understand you correctly, the real problem is creating the color information, which is, creating a bivariate histogram. Luckily, MATLAB has a function, hist3, for that in the Statistics & Machine Learning Toolbox. The syntax is
[N,C] = hist3(X,nbins)
where X is a m-by-2 matrix containing the data, and nbins is a 1-by-2 vector containing the number of bins in each dimension. The return value N is a matrix of size nbins(1)-by-nbins(2), and contains the histogram data. C is a 1-by-2 cell array, containing the bin centers in both dimensions.
% Generate sample data
X = randn(10000, 1);
Y = X + rand(10000, 1);
% Generate histogram
[N,C] = hist3([X,Y], [100,100]);
% Plot
imagesc(C{1},C{2},N);
set(gca,'YDir','normal');
colormap(flipud(pink));
colorbar;
Result:

Plotting histogram of a slice of a matrix

I have a matrix amp containing 10 row signals [1*1001]. So the total dimension of my data is [10*1001].
Each row is containing amplitude fluctuations(signal). Now by using plot(f,abs(amp)), I am plotting all 10 signals on the f vector which is having length of [1*1001].
This f vector is a frequency axes going from 70 to 110 kHz.
This is the graph which I am plotting.
figure,plot(f2,abs(amp));
xlabel('Frequency in KHz');ylabel('amp');
Now I want a histogram at 90Khz, that means at particular 90KHz frequency, how much amplitude of all 10 signals are changing?
It is somewhat difficult to understand your question, but it sounds like you would like a histogram of your row data near f = 90 KHz. If that's true, I think this should work:
%find the f2 value closest to 90000:
[f0, index] = min(abs(f2-90000));
%make a histogram of the data:
histogram(amp(:,index),10);

Matlab - multiple variables normalized histogram?

I'm working on MATLAB, where I have a vector which I need to split into two classes and then get a histogram of both resulting vectors (which have different sizes). The values represent height records so the interval is about 140-185.
How can I get a normalized histogram of both resulting vectors in different colors. I was able to get both normalized vectors in the same colour (which is indistiguible) and and also a histogram with different colours but not not normalized...
I hope you understand my question and will be able to help me.
Thanks in advance :)
Maybe this is what you need:
matrix = [155+10*randn(2000,1) 165+10*randn(2000,1)];
matrix(1:1100,1) = NaN;
matrix(1101:2000,2) = NaN; %// example data
[y x] = hist(matrix, 15); %// 15 is desired number of bins
y = bsxfun(#rdivide, y, sum(y)) / (x(2)-x(1)); %// normalize to area 1
bar(x,y) %// plots each column of y vs x. Automatically uses different colors

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')