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.

 Akzeptierte Antwort

Jack
Jack am 12 Nov. 2018

1 Stimme

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 Kommentare

Erik den Boer
Erik den Boer am 20 Nov. 2018
Hi Jack, I posted a comment last week but for some reason it didn't go through, I see now. Internet can be a bit spotty here, maybe that was it.
Anyway, thanks for your reply. The reshape command does what I want it too indeed, except I now have a 271x3x246x4 matrix. The first three sizes seem correct, but why is the 4 required? I would interpret that as the number of 'results', and expected it to be 1 since each xyz location of course has just a single value?
In any case the code works, until I get to the concatenating for variable d. My X and Z are 246x271, but my c is 271x1x246x4, so Matlab is complaining that the dimensions aren't consistent. Do you have any ideas how I can fix this?
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
Erik den Boer
Erik den Boer am 21 Nov. 2018
Ah I thought it might be indeed, but swapping it for a 1 still made things crash, and since I didn't really know what it was doing, I couldn't fix it. This code does give me a plot tho, so thanks for the help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by