How to make a histogram using accumarray and surf with decimal values on the axes?

7 views (last 30 days)
I have some code:
#My data
vec1=...;
vec2=...;
#Interpolation points
intptsvec1=linspace(2,4,21);
intptsvec2=linspace(-0.135,0.145,29);
#Interpolate points to nearest bin
intvec1=interp1(intptsvec1,1:numel(intptsvec1),vec1,'nearest');
intvec2=interp2(intptsvec2,1:numel(intptsvec2),vec2,'nearest');
#Collect bins
Z = accumarray([intvec1 intvec2], 1, [21 29]);
#Make histogram
surf(Z)
This makes a nice looking plot with axes 1..21 and 1..29. However, I want the axes to reflect the interpolation points that I chose rather than the number of the bin such that they become -0.135..0.145 and 2..4.
Any help is greatly appreciated.
  2 Comments
Shawn
Shawn on 3 Dec 2014
Is there a 3D histogram? The doc does not seem helpful for 2 inputs and a third dimension that is the "counter". This plot is exactly what I would like if the axes reflected my values. The colors dictate the "counting" or I can get a 3D output with depth.

Sign in to comment.

Accepted Answer

Shawn
Shawn on 3 Dec 2014
Using hist3 appears to be the way to go for my problem.
X=[vec1 vec2];
hist3(X,{2:0.1:4 -0.135:0.01:0.145})
set(gcf,'renderer','opengl')
set(get(gca,'child'),'FaceColor','interp','CDataMode','auto')

More Answers (1)

Image Analyst
Image Analyst on 3 Dec 2014
Not exactly sure what you mean by 3D histogram. I suspect you mean a 2D histogram like graycomatrix gives you. See my answer here: http://www.mathworks.com/matlabcentral/answers/123720#answer_131379
If you have two independent indexes and a value, that is a 2D matrix, not a 3D matrix even though you might render it as a "3D" bar chart. It's like that for any matrix. A 2D matrix M has a row, a column, and a value at each row and column. This does not make it a 3D matrix, even if you plot it with surf() or any other visualization you care to make. Don't worry - it's a common misunderstanding.
  2 Comments
Shawn
Shawn on 3 Dec 2014
I will look into your grayco matrix solution, but I'm not sure if you are correct in your defining 2D and 3D histograms here? I did not realize there is a hist3 function. From the documentation, "hist3 Three-dimensional histogram of bivariate data". So now I'm confused as to whether this should be termed a 2D or 3D histogram.
Image Analyst
Image Analyst on 3 Dec 2014
I don't have hist3 - it's not in base MATLAB any of my toolboxes. The 3 may refer to the rendering appearance rather than the dimensionality of the input.
To find out how many dimensions something has, look at how many indexes the variable can take.
1-D
I don't think anyone would dispute the fact that a row or column vector like [2,6,9,54,2,4,5] is a 1-D entity. Yet it has both a location (index, or "x" value) and a value ("y" value). Now just because I can plot the values on an x-y plot on a 2D piece of paper does not suddenly turn my row vector into a 2D entity. The array takes only one index - one independent variable - and thus is a 1D array not matter how its visualized.
2-D
Same for a grayscale image - it has two indexes, row and column, so it's 2D. It does not matter if you display it as a 2D flat gray scale image or as a surface where the height above some plane is proportional to the value of the image (the gray level) at that point -- it's still a 2D variable no matter that it looks "3D-ish" when plotted with surf. Just because it has a "dependent" value for each pair of independent values or looks 3D-ish in some visualization does not turn it into a 3D array.
3-D
What about a flat color image? There's a row and c column, and a color, which is 3 numbers for red, green, and blue. Is that a 2D image? Well it certainly looks 2D when displayed with imshow(). But mathematically and by computer variables, it's a 3D image. Because you specify 3 things: an x, a y, and a color channel (1,2, or 3 for red, green, or blue respectively). It's not 5D because it has a 2D location and 3 color values for that location. No, it's still 3D.
Let's look at a higher spatial dimension (3 instead of 2) but fewer radiometric dimensions (1 instead of 3). Let's say you have a volumetric image that is a 3D CT image of your body. So for every (x,y,z) location, you have a value. It can be stored in a 3D array CTBodyImage(row, column, slice). If you specify a row and a column and a slice number, you will get the radiographic density at that location. It's not a 4D image because it has a value at a location. It's still a 3D image.
4-D
What if we had a visible light microscope image of a 3D thing, like an insect. Let's say you microtomed it so you had full color images at every slice. So every (x,y,z) location has 3 colors associated with it. How many dimensions it that? Is it 3? Is it 6? No, it's 4. To get the value of the color at some location, you specify 4 things: the x, the y, the z, and the color channel number (1=red, 2=green, 3=blue). So it can be specified by using a 4D array because there are 4 independent variables and 1 dependent variable.
Bottom line: look at how many indexes the variable takes, which is the same as ndims(yourArray). That's how many "D" your dataset is.
I hope this explains this often misunderstood concept.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!