Binning 2D set of coordinates

13 ビュー (過去 30 日間)
Akhil Ghanta
Akhil Ghanta 2015 年 6 月 19 日
編集済み: Walter Roberson 2016 年 10 月 13 日
I'm pretty new to MATLAB, so sorry if this seems easy. I've got a single long trajectory for a particle below. I have about 100,000 X and Y coordinates from which I've assembled the graph. I'm trying to create a density plot of the coordinates to show where the trajectory is most concentrated. I'm trying to create a density plot of the coordinates to show where the trajectory is most concentrated. I've got the second plot using the following code:
bins = 200;
binsizeX = 3/bins;
binsizeY = 1.2/bins;
xbins = -1.5:binsizeX:1.5;
ybins = -0.6:binsizeY:0.6;
[nx,idxx] = histc(X,xbins);
[ny,idxy] = histc(Y,ybins);
out = accumarray([idxx,idxy], 1);
figure(2);
hold on;
h=imagesc(xbins,ybins,out);hold on;
colorbar; hold on;
axis([-1.5 1.5 -0.6 0.6]); hold on;
But it seems that the density is shifted up and to the right for some reason? Any help is appreciated! Thanks!

回答 (4 件)

alessandro
alessandro 2016 年 10 月 13 日
編集済み: Walter Roberson 2016 年 10 月 13 日
Old post, but will try to answer anyway, in case anybody else end up with same issue.
The easiest way to do what you are willing to do is to use hist3 function, with which binning resolution is user dependent and density matrix can be stored easily.

Katalin
Katalin 2015 年 6 月 19 日
imagesc flips the image I think. This is from the help: imagesc(x,y,C) displays C as an image and specifies the bounds of the x- and y-axis with vectors x and y. If x(1) > x(2) or y(1) > y(2), the image is flipped left-right or up-down, respectively.
  1 件のコメント
Akhil Ghanta
Akhil Ghanta 2015 年 6 月 19 日
編集済み: Akhil Ghanta 2015 年 6 月 19 日
If that's true why are the axes still going in the proper direction on the plot? Also, for my vectors x(1)<x(2) and y(1)<y(2)

サインインしてコメントする。


Image Analyst
Image Analyst 2015 年 6 月 19 日
With images, since they are arrays, line 1 (row 1) is at the top, and the line numbers get bigger as you go down the screen. This is the standard custom with images.
With a plot, since it's like regular Cartesian coordinates, where the "y" value increases as it goes up the screen.
The right/left are not flipped, just the up/down. You could use flipud(out) to flip the image vertically before displaying it if you want the plot and image to look similar.
imagesc(xbins, ybins, flipud(out));
  2 件のコメント
Akhil Ghanta
Akhil Ghanta 2015 年 6 月 19 日
編集済み: Akhil Ghanta 2015 年 6 月 19 日
Well now, the density is just flipped. The slope of the ellipse is now negative. The big thing is that the density isn't centered like the original trajectory is in the original post.
Image Analyst
Image Analyst 2015 年 6 月 20 日
Is the centroid supposed to be at (0,0)? Please attach your data file with the paper clip icon, along with code to read it in.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2015 年 6 月 19 日
out = accumarray([idxx,idxy], 1);
should be
out = accumarray([idxy,idxx], 1);
Height (y) is expressed as row number so the y index needs to be first.
  2 件のコメント
Akhil Ghanta
Akhil Ghanta 2015 年 6 月 19 日
This did seem to fix the slope of the density ellipse, but it's still not centered on the origin like I'd expect it to be.
Walter Roberson
Walter Roberson 2015 年 6 月 20 日
Could you attach the data as a file?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by