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!