How to make a histogram using accumarray and surf with decimal values on the axes?
6 views (last 30 days)
Show older comments
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
Accepted Answer
More Answers (1)
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
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.
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!