How to create a histogram without using the matlab function

조회 수: 2 (최근 30일)
Ole
Ole 2020년 2월 28일
댓글: Ole 2020년 3월 1일
How to create a histogram without using the matlab hist function.
Given scattered data x = randn(1,100); y = randn(1,100);
with coresponding phase p = randn(1,100), having phase phase = exp(1i*p);
would like to create uniform grid, add the phase for the data points that are inside each bin.
This is to create a na intensity plot or coherent sum of the scattered data.
  댓글 수: 7
Guillaume
Guillaume 2020년 3월 1일
@Ole, can you give a formal mathematical definition of what it is you want to calculate for a bin, because as you can see we're a bit confused.
Ole
Ole 2020년 3월 1일
There is scatted data (points) in space given as vectors x, y. Let say we discretize the data and in each bin (pixel) fall a set of points {x(k), y(k)}. Each point x(k), y(k) is associated with phase exp(1i*p(k)). In each bin (n,m) I would like to sum the phases for the points that fall inside the bin {x(k), y(k)} -> sum( exp(1i*p(k)) ). So the out put is a matrix M(n,m) that is with reduced size because of the binning and the values of the matrix are M(n,m) = sum( exp(1i*p(k)) ). And would like also to have the number of points x(k), y(k) that are in n,m bin (what histogram does).

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2020년 3월 1일
As Steven said, use discretize to find the bin indices and then one of the many aggregation functions in matlab. With 3 vector inputs, I'd use the older accumarray:
%demo data and bin definitions
x = randn(1, 100);
y = randn(1, 100);
p = randn(1, 100);
phase = exp(1i*p);
xedges = [-Inf, -3:3, +Inf]; %there will be one less bin that there are edges
yedges = [-Inf, -3:3, +Inf]; %see documentation of discretize
%histogram building
destrow = discretize(y, ybins);
destcol = discretize(x, xbins);
phasehistogram = accummaray([destrow, destcol], phase, [numel(ybins), numel(xbins)] - 1);
Or you could put the vectors in a table, and call groupsummary which would do the binning and summing for you:
%demo data and bin definitions
x = randn(1, 100);
y = randn(1, 100);
p = randn(1, 100);
phase = exp(1i*p);
xedges = [-Inf, -3:3, +Inf]; %there will be one less bin that there are edges
yedges = [-Inf, -3:3, +Inf]; %see documentation of discretize
%table construction and histogram:
phasetable = table(x, y, phase);
phasehistogram = groupsummary(phasetable, {'x', 'y'}, {xedges, yedges}, 'sum', 'phase');

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 2월 28일
Consider using discretize to bin the data then passing that grouping information into groupsummary or splitapply.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by