Extract data using histogram2()

조회 수: 21 (최근 30일)
sagun subedi
sagun subedi 2020년 7월 1일
답변: Image Analyst 2020년 7월 1일
Is it possible to extract data (x-coordinates and y-coordinates) using the syntax histogram2()?

채택된 답변

sagun subedi
sagun subedi 2020년 7월 1일
%%%%input
location = rand(10, 2); %%%representing x and y coordinates
[N, x,y,binx,biny] = histcounts2(location(:, 1), location(:, 2));
%%%%output
N = 2×2
3 5
1 1
x = 1×3
0 5 10
y = 1×3
0 5 10
binx = 10×1
1
1
1
1
1
1
1
2
1
2
biny = 10×1
2
2
1
1
1
2
2
1
2
2
Number of count is first bin N(1) = 3 and so on. My question is how to get the actual coordinates as shown in the figure above for each bin with edges x and y.
  댓글 수: 1
Steven Lord
Steven Lord 2020년 7월 1일
isInFirstBin = (binx == 1 & biny == 1);
pointsInFirstBin = location(isInFirstBin, :)
isInBin12 = (binx == 1 & biny == 2);
pointsInBin12 = location(isInBin12, :)

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

추가 답변 (2개)

Steven Lord
Steven Lord 2020년 7월 1일
If you want the data that was passed into histogram2 retrieve the Data property of the histogram.
If you want the edges of the bins, get the XBinEdges and YBinEdges properties.
For the bin counts (raw or with the specified Normalization applied) see the BinCounts and Values properties.
If none of those are what you mean by "data" please clarify what specifically you want to extract.
  댓글 수: 4
sagun subedi
sagun subedi 2020년 7월 1일
I am really sorry, but this is not working either.
Steven Lord
Steven Lord 2020년 7월 1일
Please show a small sample of your data, show how you called histcounts2 on that data, and explain what "not working" means. Did it not give you the results you expected? Did it throw an error and if it did what was the full text of that error, all the text in red? With this information we may be able to help you do what you asked.

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


Image Analyst
Image Analyst 2020년 7월 1일
Try this:
clc; % Clear the command window.
fprintf('Beginning to run %s.m.\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
numPoints = 20;
xy = rand(numPoints, 2); % Representing x and y coordinates
plot(xy(:, 1), xy(:, 2), 'b.', 'MarkerSize', 20);
grid on;
xEdges = [0, 0.5, 1];
yEdges = [0, 0.25, 0.5, 0.75, 1];
xticks(xEdges);
yticks(yEdges);
xlim([min(xEdges), max(xEdges)]);
ylim([min(yEdges), max(yEdges)]);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
counts = histcounts2(xy(:, 1), xy(:, 2), xEdges, yEdges)
% With counts, the x counts are rows and the y are columns.
% Place the count in each bin
for kx = 1 : size(counts, 1)
xt = mean([xEdges(kx), xEdges(kx + 1)]);
for ky = 1 : size(counts, 2)
yt = mean([yEdges(ky ), yEdges(ky + 1)]);
textLabel = sprintf('Count = %d', counts(kx, ky));
text(xt, yt, textLabel, 'Color', 'r', 'FontSize', 12, 'HorizontalAlignment', 'center');
end
end
fprintf('Done running %s.m.\n', mfilename);

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by