How to not view a bar in histogram ? using MATLAB - 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)

Related

PCA for image processing

I want to get the first principal component for an image using the built-in function pca. How can I do that?
I have tried the following code:
[COEFF, SCORE] = pca(image);
SCORE(1:size(SCORE,1),:)=0;
reconstructed_image = SCORE / COEFF + repmat(mean(image,1),size(image,1), 1);
I=reshape(reconstructed_image,[256,256]);
figure
imshow(I,[0 255])
I only get the fist row of the image. Any idea how can I do that correctly?
You can't "PCA one image". What this did is not give you the first row, it used all rows as observations and your columns as parameters, like you'd usually set up your measurements. So it calculated the variance through all parameters, giving you a vector with the length being equal to your number of columns. You'd probably want more images to do this instead.
Please read the following answer of mine though before continuing, since I explain the main pitfalls of PCA in MATLAB there.
PCA in matlab selecting top n components

How to imshow() to show basis images for hadamard transformation in MATLAB?

I have successfully written the MATLAB code for finding the n-order hadamard matrix.
Then I found the transpose of that matrix.
Then I found the basis images,
e.g., A(1,3)th basis image = hadamard_matrix(:,1)*hadamard_matrix(:,2)'
But whenever I try to print it using imshow() function in matlab, it shows just a completely dark image for all the basis images.
So what is the correct approach to show such basis images in matlab ?
Thanks in advance!
The only reasonable explanation I can think of right now, is that your resulting matrix contains only of values smaller than, say, 0.05.
Instead of the default bounds 0 and 1, try other high/low values. For instance:
imshow(A,[min(min(A)) max(max(A))]);

Matlab gmdistribution.fit only finding one peak

I have an array named Area, which contains a set of values.
The histogram of the array looks like this
The bin width is 60 in this case. I'd like to fit two gaussians to the two peaks here (even if it won't be a great fit).
So I used:
options = statset('Display','final');
obj = gmdistribution.fit(area,2,'Options',options);
gausspdf = pdf(obj, xaxis);
A = sum(gausspdf);
gausspdf = gausspdf/A;
But when I try to plot the two fitted Gaussians, the resulting curve looks like this:
I'm quite confused, as there should be two peaks appearing in the plot?
The gmdistribution.fit method fits data according to maximum-likelihood criterion; that is, it tries to find parameters which maximize the likelihood given the data. It will not necessarily fit what you see or expect visually. Still, there is the possibility that the algorithm converged to a "bad" local minimum. You can try and set the initial conditions according to what you want to get, practically 'helping' the algorithm to converge to the desired result. You do this using the Start option to the fit method, which enables you to give it either an initial guess, in which case you should try and estimate the parameters from the histogram, or an initial component index for each data sample. See the documentation for more details.
I think that your peaks are too close and the function can't distinguish them. so maybe you should change the options for gmdistribution or apply a non-linear function to your data first to get more separate peaks in histogram.

Plotting from 3D matrix in Matlab

I have a matrix which is 1*1*10000, the slightly odd dimensions are the result of the matrix algebra used to calculate it.
I simply want to be able to plot the 10000 data points contained in it, but matlab seems unable to do it?
Can someone please tell me how I can plot the data?
Seems simple but I really can't figure out how to do it!
Baz
yes you need to reduce the dimensions to a vector:
A = zeros(1,1,100)
vector = squeeze(A(1,1,:))
as when you'd access the third dimension this would only return a 3D-Matrix again:
z = A(1,1,:)
would NOT work. So use squeeze() ;-) Then plot as usual.
Doc-Link: http://www.mathworks.de/de/help/matlab/ref/squeeze.html
And as Ander pointed out in comments, no need to give any dimensions, as it removes singleton-dimensions by itself. So just use vector = squeeze(A). MATLAB recognizes the way to go itself.

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};