How to obtain histogramof each individual matrix in cell array?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I have a 8 by 8 cell and each cell consist of a matrix with 1 row and 49 columns.how do i compute histogram for each cell using a for loop?
I would like to plot for each matrix in the cell array and meaning i will obtain 64 figures of histograms as i have 64 cells. How do I access each matrix to plot the histogram?
Accepted Answer
In its simplest form:
cellfun(@histogram, yourcellarray);
If you want to specify the number of bins or edges
nbins = 5;
cellfun(@(c) histogram(c, nbins), yourcellarray);
8 Comments
Jupiter
on 15 Dec 2015
Sorry if my question is unclear.
I would like to plot for each matrix in the cell array and meaning i will obtain 64 figures of histograms as i have 64 cells. How do I access each matrix to plot the histogram?
Thanks for your help
Stephen23
on 15 Dec 2015
That is what Guillaume's code does. Did you try it?
Guillaume
on 15 Dec 2015
Well, it is what my code do, but not what I intended actually. I meant to use histcounts and not histogram. But, yes, my code answers the question exactly.
I've just the superfluous 'UniformOutput', since there's actually no output.
@Kimberly, you can write exactly the same thing with a loop, the first cellfun is exactly equivalent to:
for celliter = 1:numel(yourcellarray)
histogram(yourcellarray{celliter});
end
Jupiter
on 15 Dec 2015
Yup..I tried. I have an error saying Undefined function or method 'histogram' for input arguments of type 'uint8'.
I am sorry as I am new in matlab. Thanks for your kind explanation
Guillaume
on 15 Dec 2015
You must be using an old version of matlab (pre r2014b).
Replace histogram by hist.
Jupiter
on 15 Dec 2015
I am using r2010a.
after using cellfun(@hist,totalblocks1);
I get an error writing:
??? Error using ==> times Integers can only be combined with integers of the same class, or scalar doubles.
Error in ==> hist at 78 xx = miny + binwidth*(0:x);
Error in ==> imageblocksmatrix at 264 cellfun(@hist,totalblocks1);
This latest error occurs because your matrices are of type uint8 instead of double and old versions of matlab weren't good at coping with non-double numerics. A simple way to fix this:
cellfun(@(c) hist(double(c)), totalblocks1);
The same, using a loop:
for celliter = 1:numel(totalblocks1)
hist(double(totalblocks1{celliter}));
end
I would recommend you move to a newer version of matlab, if possible. A lot has been improved since 2010.
Jupiter
on 16 Dec 2015
It is working now. Thanks alot for your help!
More Answers (0)
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)