Doug, I am a little confused…I assume x and y are just random coordinates…you then created xd, yd…what is this actually?? How did you do it? Then in linespace you use x,y again, but in interp you use xd,yd. I think I understand the concept, but I’m lost in the details. Can you post all the code so I can run what you did??
Thanks for the tutorial. Nice thing, I never came up against accumarray which seems to be very useful!
But actually there is a mistake when calculating the BIN-number: since interp1 assigns the nearest value in every dimension, all data points say in x-direction belonging to BIN 1 and half of them in BIN 2 will be assigned to (1,:). Correct should be interpolating to
xr = interp1(xi,0.5:numel(xi)-0.5,xd,'nearest')';
yr = interp1(yi,0.5:numel(yi)-0.5,yd,'nearest')';
In the example code, the data from xd will be plotted on the Y axis, and yd on the X axis. To get a properly labeled graph, with the X data along the X axis, you actually need to say:
surf(xi, yi, Z');
Why is that? In the example, we have
Z = accumarray([xr yr], 1, [n n]);
For instance, if something falls in the 1st X-bin and the 5th Y-bin, it will get counted in Z(1,5). But, the MATLAB plotting commands like SURF and CONTOUR take their inputs in the opposite way: To have something show up at (1,5) in the graph, it should be in Z(5,1).
Hope this is useful to the next person who’s thinking sideways!
Thanks for the video! I was able to use it effectively for 2 vectors (xd & yd).
I had some other questions about the extent to which I could use this code, or if there are other options available.
1. Is there an alternative to accumarray that will take negative inputs -or am I able to edit accumarray in a way that would allow it to take negative inputs?
2. Is there any way to have such a histogram plot for 3 vectors (xd, yd and zd)? What I would like to have is the x and y coordinates with z as the height and color coded by the frequency distribution. I am basically trying to visualize a special euclidean(2) space (since my z vector lies on polar coordinates). Is there any way to adapt your code to work for 3 vectors in this way?
I know these questions don’t directly apply to the working of the posted tutorial, but any suggestions would be very much appreciated!
Thanks in advance!
Hi there, I would like to ask if you know how to label the peak values of the data points in your 3D plot. I can’t seem to find any commands in MATLAB that are able to perform that function. Thank you.
Thank you very much for this post. Its wonderful and exactly what I need to look some 3.5 million data points I have. So I have gone through each comments and details on this page and summarized the code as given below. But I am getting an error “Error using accumarray, First input SUBS must contain positive integer subscripts.” I know there are no negative values in xr and yr, and as suggested I am adding 0.5. So how can I fix it? Please help. Thanks in advance.
x = MAge000(:);
y = DSTS000(:);
% NaN values taken out by
x = x(isfinite(x(:, 1)), :);
y = y(isfinite(x(:, 1)), :);
% Number of bins
n = 49;
xi = linspace(min(x(:)), max(x(:)), n);
yi = linspace(min(y(:)), max(y(:)), n);
xr = interp1(xi, 0.5:numel(xi)-0.5, x, ‘nearest’);
yr = interp1(yi, 0.5:numel(yi)-0.5, y, ‘nearest’);
% [yr xr]: Y then X because the plotting commands take matrices that look like Z(y-coord, x-coord)
Z = accumarray([yr xr] + 0.5, 1, [n n]);