Extract data using histogram2()

18 vues (au cours des 30 derniers jours)
sagun subedi
sagun subedi le 1 Juil 2020
Is it possible to extract data (x-coordinates and y-coordinates) using the syntax histogram2()?

Réponse acceptée

sagun subedi
sagun subedi le 1 Juil 2020
%%%%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 commentaire
Steven Lord
Steven Lord le 1 Juil 2020
isInFirstBin = (binx == 1 & biny == 1);
pointsInFirstBin = location(isInFirstBin, :)
isInBin12 = (binx == 1 & biny == 2);
pointsInBin12 = location(isInBin12, :)

Connectez-vous pour commenter.

Plus de réponses (2)

Steven Lord
Steven Lord le 1 Juil 2020
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 commentaires
sagun subedi
sagun subedi le 1 Juil 2020
I am really sorry, but this is not working either.
Steven Lord
Steven Lord le 1 Juil 2020
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.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 1 Juil 2020
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);

Community Treasure Hunt

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

Start Hunting!

Translated by