quantaize data and build an histogram

2 views (last 30 days)
michael
michael on 29 Oct 2021
Answered: Ganesh on 14 Jun 2024
Hello,
I have a 3D data (X,Y,Z). Where X is between 0 to 360, Y is between 0 to 180 and Z is between -100 to + 100.
I'd like to quantize the data to NxM scale values on X and Y axes respectively and to have some mean value for Z.
This data shall be displayed on 2D plot where the Z values shall be represented by colors (if value is close to 100, then green, if the value is close to -100 then red).
How this could be done?

Answers (1)

Ganesh
Ganesh on 14 Jun 2024
You can use the following code to achieve what you are trying to do:
X = randi([0, 360], 1000, 1);
Y = randi([0, 180], 1000, 1);
Z = randi([-100, 100], 1000, 1);
Nx = 36;
M = 18;
edgesX = linspace(0, 360, Nx+1);
edgesY = linspace(0, 180, M+1);
quantizedX = discretize(X, edgesX);
quantizedY = discretize(Y, edgesY);
uniquePairs = unique([quantizedX, quantizedY], 'rows');
meanZ = zeros(size(uniquePairs, 1), 1);
for i = 1:size(uniquePairs, 1)
idx = quantizedX == uniquePairs(i, 1) & quantizedY == uniquePairs(i, 2);
meanZ(i) = mean(Z(idx));
end
Zgrid = nan(Nx, M);
for i = 1:size(uniquePairs, 1)
Zgrid(uniquePairs(i, 1), uniquePairs(i, 2)) = meanZ(i);
end
figure;
imagesc(Zgrid);
colorbar;
colormap([linspace(1,0,256)', linspace(0,1,256)', zeros(256,1)]);
caxis([-100 100]);
xlabel('Quantized X');
ylabel('Quantized Y');
title('Mean Z Values Represented by Color');
axis xy;
Here, we primarily make use of the "discretize()" function to divide the X and Y data into bins. Through a loop, we process each of the bins and assign the corresponding mean value of Z to the X and Y data. We then make a "Zgrid()", which would help us in plotting the required colour map.
You can find the documentation for each of the mentioned functions below:
Hope this helps!

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Products


Release

R14SP2

Community Treasure Hunt

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

Start Hunting!