How to calculate total mass of points in each 1cm thick slice of a rectangular cuboid?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
I have a rectangular cuboid in which I have a cloud of points, each having a mass assigned to it. Input file ('file_%d.txt') provides the following data regarding these points:
column 1: point type (1, 2, 3 …)
column 2: x coordinate
column 3: y coordinate
column 4: z coordinate
column 5: mass of the point
There are over 10k points listed in a file.
What I want to do is to go along z direction which is 100cm long, cut it into 1cm thick slices (along z, so 100 slices in total, in the code they are named regions) and for each slice calculate the total mass of the points which are in that specific slice. Also, I have 3 input files that contain these points and I need to obtain average mass for each slice over these 3 files. Then I would like to export that average mass over each slice as a function of the distance from 0 to 100cm (which corresponds to slice numbers) to either a .txt or .xlsx file.
Last time I did some programming in Matlab was ages ago – the code below is needed for data processing. I got stuck with the for & if loops and would be super grateful for your help guys.
mini=1; inc=1; maxi=3;
regions = zeros(1,100)
dr=1 %length of each slice along z (so slice thickness is 1)
c=0;
for i=1:length(mini:inc:maxi) %loop over files which are to be processed
c=c+1;
str = sprintf('file_%d.txt',mini+(i-1)*inc);
temp = importdata(str,' ',9); %read data only (starting from line 9 which is where the data appears)
data = sortrows(temp.data) %order data in ascending order with respect to the 1st column (which is a number that describes type of thing, e.g. 1=red, 2=blue, etc.)
% below: var_(…) ---> bottom / top value of the length of each region, e.g. from 1 to 2, from 2 to 3, etc.
var_bottom=0
var_top=1
fork=1:length(data)
var_bottom=var_bottom+1
var_top=var_top+1
if data(j,4)=<dr*var_top && if data(j,4)>dr*var_bottom
total(k+1)=sum(data(j,4))
???????? – not sure if the for loop above makes sense and, on top of that, my use of && was deemed “illegal” by Matlab. Also, not sure how I should code the rest of what I need
end
fclose(fileID);
c
regions = regions/c; % not sure if I need this one
regions = regions/dr
x = 1:dr:length(regions)
plot(mass,regions, '-.')
for k = 1 : length(mini:inc:maxi)
filename = sprintf('position_vs_mass_%d.xlsx', mini+(i-1)*inc);
xlswrite(filename, [mass(:),regions(:)]); % not sure if I can do averaging here or it needs to be done in the code above
end
0 件のコメント
回答 (1 件)
darova
2021 年 3 月 18 日
Here is a simple example using rounding
clc,clear
x = rand(100,1);
y = rand(100,1);
% let's divide Y coord into 5 regions [ 1 2 3 4 5];
c = round(5*y+1);
% data to create lines
xx = repmat([0; 1],1,6);
yy = [0:5; 0:5]/5+0.1;
scatter(x,y,50,c,'filled')
line(xx,yy,'color','b')
参考
カテゴリ
Help Center および File Exchange で Lighting, Transparency, and Shading についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!