How to plot 3D data values in a 2D map?
11 views (last 30 days)
Show older comments
Hello,
For a project I am working on, I have a truckload of data. For a few hundred thousand points in a room, I have x-, y- and z-coordinates and a value of the amount of radiation there. Here, y is the height and x and z are the width and breadth of the room. I want to plot this in a top-view, so adding all y-values for a certain (x,z) together, and then plotting the resulting values in a heat map to show where there is a lot of radiation and where you're fairly safe.
My data is of the format (x, y, z, value):
1 1 1 v1
1 1 2 v2
1 1 3 v3
1 1 4 v4
1 2 1 v5
1 2 2 v6
1 2 3 v7
1 2 4 v8
2 1 1 v9
etc.
I already have variables for the unique number of x-, y- and z-points. Let's say I sampled 5 x-, 4 y- and 3 z-locations. I would like to write a loop that automates the addition of, in this scenario, v1, v4, v7 and v10 (the row increases by the number of z-points and the number of values that are added together are the number of sampled y-points), and then of v2, v5, v8, v11, etc. Is there any easier way than three nested for-loops? For now, that is all I can come up with, but I hope there's a more straightforward way of getting the plot I want.
0 Comments
Accepted Answer
Jack
on 12 Nov 2018
a = your_data;
b = reshape(b, n_x, n_y, n_z, 4); %n_i is the size of the ith grid direction
c = sum(b,2); %sum in the y direction
[X,Z] = meshgrid(1:n_x, 1:n_z);
d = [X(:),Z(:),c(:)];
Takes ~0.1s for a 100x100x100 data set
3 Comments
Jack
on 20 Nov 2018
Hi Erik, I don't know why I put a 4 before. Looking at my code:
tic
% generate random values
n_x=100;
n_y=100;
n_z=100;
[Z,Y,X] = ndgrid(1:n_z,1:n_y,1:n_x);
a=[X(:),Y(:),Z(:)];
a(:,4)=randi([0,100],n_x*n_y*n_z,1);
% run reshape
b = a(:,4); %values
b = reshape(b, n_x, n_y, n_z, 1);
c = sum(b,2);
[X,Z] = meshgrid(1:n_x, 1:n_z);
d = [X(:),Z(:),c(:)];
toc
There is no 4, so I guess it was a typo.
Thanks, Jack
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!