I have a matrix with 3 columns. The first two columns are coordinates and the third is weight or intensity.
newmat = [ 27.37 -45.69 14.47
27.37 -45.68 18.58
27.37 -45.67 29.05
27.37 -45.66 51.7
... ... ... ]
I have already created a scatter plot:
However, I'd like to have something like a density plot (as the second plot here). I have tried to use hist3
function as in here, but I didn't figure out how to take into account the third column - weight.
You could create a matrix from the data in newmat
(using the functions sortrows
, unique
, and accumarray
) and plot it as an image:
newmat = sortrows(newmat, [1 2]); % Sort the first two columns in ascending order
[x, ~, newmat(:, 1)] = unique(newmat(:, 1)); % Make numeric indices for column 1
[y, ~, newmat(:, 2)] = unique(newmat(:, 2)); % Make numeric indices for column 2
M = accumarray(newmat(:, 1:2), newmat(:, 3)).'; % Build the matrix
imagesc(x, y, M);
Here's some sample data similar to your format:
[X, Y] = meshgrid(0:0.1:2, 3:0.1:5);
Z = peaks(21);
newmat = [X(:) Y(:) Z(:)];
And here's the plot the above code produces from that data: