Compute a histogram() without plotting in matlab - matlab

Is there a way to get the result object of a call to histogram() without the histogram plot?
I want to use the results in a script without figures being generated.
Thanks.

On octave you can use
values = hist(...);
without generating a plot. hist will also return the bin centers if you provide a second output argument. Note that Mathworks recommends using histogram and histcounts (see below) instead. You may also be interested in histc which takes the position of the bin edges as an input.
On current Matlab versions you can also use histcounts to get the bin counts and edges.
Please see also the documentation of hist and histcounts.

Related

Wrong histogram with built in hist() function

bootstrap.rand is a matrix with 253x10000 integer values ranging from 1 to 253.
The built-in hist() command returns:
hist(bootstrap.rand)
When building a histogram by using the barplot command I get a complete different result:
bar(histc(bootstrap.rand(:),unique(bootstrap.rand)))
Because of the y-axis in the first picture obviously it does not do what i want. Why this difference?
The functions hist and histc are not recommended by MATLAB:
hist is not recommended. Use histogram instead.
For more information, including suggestions on updating code, see Replace Discouraged Instances of hist and histc.
Instead use histogram which gives the wanted output:
bootstrap = randi(253,253,10000);
histogram(bootstrap)
The shape of bootstrap doesn't matter, it will always be treated as bootstrap(:).

How to make a Q-Q Plot with a Student-t distribution

Working in Matlab...
I have a long vector of data vec that I want to make a Q-Q plot of against a Student-t distribution for various values of t. I know qqplot(vec) produces a plot against a normal distribution. I know from here that I can use other distributions by inserting them as a second parameter qqplot(vec,dis).
I can make other distributions with makedist (see mathworks.com/help/stats/makedist.html) but Student-t isn't an option there that I can see. There are the functions tpdf, tcdf etc. about Student-t (see http://www.mathworks.com/help/stats/students-t-distribution-1.html) but how do I use them to make a distribution item to use in the Q-Q plot? Or is there another technique to get the plot I want?
I haven't used Matlab in about a decade, so I can't give specific syntax. That said, if vec has length n you should be able to create a vector of t-values corresponding to quantiles i / (n+1) for i=1,...,n using tinv. Then do a qqplot of vec vs. the t-vector.

How to not view a bar in histogram ? using MATLAB

I have drawn a histogram of a variable I using the function hist available in Matlab R2012b.
hist(I(:),100);
I have got the following result, it is a histogram :
The problem is as follow: I don't care about the values on Zero following X-axis. I would like to draw a histogram without putting focus on the huge value of zeros.
I found this solution :
[counts,centers] = hist(I(:));
[~,i] = max(counts);
counts(i)= 0;
bar(centers,counts);
But it seems not good one !
Is there a way to specify bins interval without zero !? is there a way, using code, to zoom in so that I can recognize clearly the other bars ?
The documentation of the function hist is available here.
Any suggestion is a welcome.
If you don't care about zeros, don't pass them to hist:
hist(I(I~=0),100)

Creating a 3D function in Matlab using meshgrid

This should be a really simple question, but for some reason I'm getting unreasonably confused and the Matlab documentation isn't helping.
Given a uniform grid of coordinates (x_i,y_j,z_k), I want to make a 3-dimensional array F in Matlab such that F(i,j,k)=f(x_i,y_j,z_k). The following is obviously incorrect:
x=linspace(-1,1,100) % uniform mesh on [-1,1]^3
[X,Y,Z]=meshgrid(x);
f=X.*Y.*sin(pi*Y.*Z) % for example
Do I need to use permute somewhere? I know that I could simply make a triple loop, but as we know that is slow.
Thanks!
Use ndgrid instead of meshgrid to avoid the unwanted permutation between first and second dimensions.
From the documentation (see also here):
MESHGRID is like NDGRID except that the order of the first two input
and output arguments are switched (i.e., [X,Y,Z] = MESHGRID(x,y,z)
produces the same result as [Y,X,Z] = NDGRID(y,x,z))

MATLAB Adding Gaussian fit to histogram

I am trying to add Gaussian fit to the histogram in MATLAB, but I do not know how to apply the fit to one particular peak only.
http://postimg.org/image/phms61rdh/ first plot
http://postimg.org/image/sindrn8er/ second plot
I am also posting part of the code I have been using:
data_Rb = (importdata('path\name.txt'));
counts_Rb = data_Rb.data(:,3);
figure
hist(counts_Rb, nbins);
xlim([0 120]);
title('Rubidium');
histfit(counts_Rb,1000);
pd=fitdist(counts_Rb(:),'normal')
x=0:0.001:120;
PDF=pdf(pd,x); %PDF is a vector of y values: it's our fit
PDF=PDF/max(PDF); %nor
y=ylim;
PDF=PDF*y(2);
hold on
plot(x,PDF,'r-','LineWidth',2);
hold off
These two blocks give me two different Gaussians as shown in the first picture. I do not understand why they fit data so badly: is it because of the tail on the RHS?
In the second plot I need to apply Gaussian fit to the last peak only. How should I do it?
And finally, after applying the fit, fit results are outputed onto the screen. Is there a function to save them into an array or so for later use?
Thanks in advance!
Regarding your last question, see How can I access the fitted distribution parameters from the HISTFIT function in Statistiics Toolbox ?
There is a workaround for this isssue by making a copy of the HISTFIT
function and modifying the code to pass out the "pd" variable. One way
to do this is to change the "h" output on the first line of the code
to "varargout" and add the following to the end of the file:
h = [hh; hh1];
argout={h,pd};
if nargout > length(argout)
error('Too many output arguments.');
end
[varargout{1:nargout}]=argout{1:nargout};