How to calculate sum area of a particular coordinate?

9 views (last 30 days)
Hi all,
If I have table below here,
Area Centroid
____ ________________
9 147.67 109.11
27 146.81 108.52
43 147.09 109.7
54 147.81 109.35
12 146.17 134.42
56 147.73 109.46
21 146.48 134.05
53 147.83 109.26
20 147.3 134.25
2 126.5 146
43 147.67 108.63
24 147 134.04
2 126.5 146
45 147.93 108.87
20 147.25 133.95
10 125.5 146.8
37 147.43 108.89
16 147.31 133.56
6 126.17 147
36 147.39 108.81
12 147.92 133.75
6 126.5 147
34 147.32 108.91
12 147.92 133.75
8 126.5 146
29 147.69 109
13 147.54 133.38
8 126.5 146
28 147.71 109.11
7 148.14 134.43
8 126.5 146
28 147.71 109.11
6 148 134.5
7 126.43 145.71
28 147.71 109.11
7 147.29 133.71
1 106 132
8 126.25 145.5
25 147.4 109.08
7 147.29 133.71
1 106 132
5 126.4 145.2
31 147.03 108.87
9 147.33 133.78
2 105.5 132
6 126.17 145
31 147.03 108.87
9 147.33 133.78
1 106 132
3 125.67 144.33
31 147.03 108.87
9 147.33 133.78
31 147.03 108.87
9 147.33 133.78
28 147.43 109.39
10 146.7 133.6
25 147.08 109.4
11 147.55 133.18
28 147.43 109.39
5 146.8 133.2
20 147.7 109.55
3 149 132
12 147.92 109.33
How to calculate the summation area of certain coordinate?
Let say I want to calculate the total area that have centroid (147 109)
I try
>>sum(147 109)
But got error
sum(147 109)
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax
error. To construct matrices, use brackets instead of parentheses.
Anyone can help me?
  8 Comments
mohd akmal masud
mohd akmal masud on 2 Oct 2021
I tried like this but the area became smaller.
Your_Answer = sum_area_objects(t, 147:148, 109:110)

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 19 Oct 2021
@mohd akmal masud your t is a table so you need to do it this way:
% Load table from mat file.
s = load('answers.mat');
t = s.t
% Calculate the sum of the areas from where (xCentroid, yCentroid) is in the range (147-148, 109-110)
rowsToInclude = t.Centroid(:, 1) >= 147 & t.Centroid(:, 1) <= 148 & ...
t.Centroid(:, 2) >= 109 & t.Centroid(:, 2) <= 110
areasInRange = t.Area(rowsToInclude)
sumOfAreas = sum(areasInRange)
Since you already have the table, t, in your program, you would not do the first 3 lines of code to read in t from a mat file.

More Answers (1)

Kevin Holly
Kevin Holly on 19 Oct 2021
@mohd akmal masud Sorry for the late reply. I was going through old comments and discovered that I wasn't following this question (so, no email notifications that comments were added).
I used scatter3 to create the 3D scatter plot.
data = [ 9 147.67 109.11
27 146.81 108.52
43 147.09 109.7
54 147.81 109.35
12 146.17 134.42
56 147.73 109.46
21 146.48 134.05
53 147.83 109.26
20 147.3 134.25
2 126.5 146
43 147.67 108.63
24 147 134.04
2 126.5 146
45 147.93 108.87
20 147.25 133.95
10 125.5 146.8
37 147.43 108.89
16 147.31 133.56
6 126.17 147
36 147.39 108.81
12 147.92 133.75
6 126.5 147
34 147.32 108.91
12 147.92 133.75
8 126.5 146
29 147.69 109
13 147.54 133.38
8 126.5 146
28 147.71 109.11
7 148.14 134.43
8 126.5 146
28 147.71 109.11
6 148 134.5
7 126.43 145.71
28 147.71 109.11
7 147.29 133.71
1 106 132
8 126.25 145.5
25 147.4 109.08
7 147.29 133.71
1 106 132
5 126.4 145.2
31 147.03 108.87
9 147.33 133.78
2 105.5 132
6 126.17 145
31 147.03 108.87
9 147.33 133.78
1 106 132
3 125.67 144.33
31 147.03 108.87
9 147.33 133.78
31 147.03 108.87
9 147.33 133.78
28 147.43 109.39
10 146.7 133.6
25 147.08 109.4
11 147.55 133.18
28 147.43 109.39
5 146.8 133.2
20 147.7 109.55
3 149 132
12 147.92 109.33];
scatter3(data(:,1),data(:,2),data(:,3),'r','filled')
xlabel('Area')
ylabel('Centriod coord 1')
zlabel('Centriod coord 2')
t=table;
t.Area = data(:,1);
t.Centroid = data(:,2:3);
Your_Answer = sum_area_objects(t,147,109)
Your_Answer = 812
Correction: "So, from 146 to 149 and 108 to 110" should be 146 to 148 and 108 to 110. I added comments to the function to clarify what was done.
"lets say I want to calculate the sum area from (147-148 109-110)"
I created a new functon to do this.
sum_area_objects_range(t,147,148,109,110)
ans = 437
sum_area_objects_range(t,146,148,108,110)
ans = 812
I'm not sure how you got an area of 2150, when the total area of all the objects added together is 1138.
sum(t.Area)
ans = 1138
function total_Area = sum_area_objects(t,coord1,coord2)
total_Area = 0;
for i = 1:height(t)
if t.Centroid(i,1) > coord1-1 & t.Centroid(i,1) < coord1+1 & t.Centroid(i,2) > coord2-1 & t.Centroid(i,2) < coord2+1
% t.Centroid(i,1) > coord1-1
% This segment checks each row of the first centroid column to see if
% the value is greater than 146, when coord1 = 147
% t.Centroid(i,1) < coord1+1
% This part does the same thing but check if the value is less
% than 148, when coord1 = 147
% t.Centroid(i,2) > coord2-1 & t.Centroid(i,2) < coord2+1
% This checks each row of the second centroid column to see if
% the value is greater than 108 and less than 110 when coord2 = 109.
total_Area = total_Area + t.Area(i);
end
end
end
function total_Area = sum_area_objects_range(t,coord1_min,coord1_max,coord2_min,coord2_max)
total_Area = 0;
for i = 1:height(t)
if t.Centroid(i,1) > coord1_min & t.Centroid(i,1) < coord1_max & t.Centroid(i,2) > coord2_min & t.Centroid(i,2) < coord2_max
total_Area = total_Area + t.Area(i);
end
end
end
  2 Comments
Kevin Holly
Kevin Holly on 19 Oct 2021
data=[5.7553e+05 127.46 134.58 72.344
1 16 138 1
4 30.25 103.25 1.75
17 31.353 112.18 2.2353
13 36.462 98.308 1.1538
71 69.268 155.39 3.3239
1 72 227 1
2 78 190.5 1
31 84.903 174.84 1.3226
21 94.333 168.38 1.1429
7 104.71 36 1
1 112 228 1
1 121 211 1
76 129.82 175.55 2.9211
1 141 85 1
9 147.89 227.22 1
24 148.88 212.29 1.375
40 152.57 156.38 3.45
318 152.14 177.12 5.9057
62 162.85 196.77 1.371
1 161 223 1
1 165 129 1
11 169.18 205.64 1.5455
2 170.5 141 1
1 175 210 1
3 200 180.33 1
47 207.7 157.11 1.4255
2 204 187.5 1
4 215.5 195.5 1
1 221 89 1
7 233.43 174.29 1.8571
1 223 172 2
1 15 137 3
7 31.286 137.57 3.2857
1 42 107 3
6 65.167 192 3
4 71.5 189 3
1 119 208 3
156 117.03 174.55 5.9872];
t=table;
t.Area = data(:,1);
t.Centroid = data(:,2:4);
data=t
data = 39×2 table
Area Centroid __________ __________________________ 5.7553e+05 127.46 134.58 72.344 1 16 138 1 4 30.25 103.25 1.75 17 31.353 112.18 2.2353 13 36.462 98.308 1.1538 71 69.268 155.39 3.3239 1 72 227 1 2 78 190.5 1 31 84.903 174.84 1.3226 21 94.333 168.38 1.1429 7 104.71 36 1 1 112 228 1 1 121 211 1 76 129.82 175.55 2.9211 1 141 85 1 9 147.89 227.22 1
Everything above was written to recreate the table that you already have. I was able to create a variable named data that was a table with 39 rows. Your table has 1682 rows.
I have modified the functions to include the 3rd coordinate of centriod. I also changed it to include the min and max values by changing > to >= and < to <=.
Here is an example looking between 80-130, 80-130, and 1-3 for the respective centroid columns.
sum_area_objects_range(data,80,130,80,180,1,3)
ans = 128
function total_Area = sum_area_objects(t,coord1,coord2,coord3)
total_Area = 0;
for i = 1:height(t)
if t.Centroid(i,1) >= coord1-1 & t.Centroid(i,1) <= coord1+1 & t.Centroid(i,2) >= coord2-1 & t.Centroid(i,2) <= coord2+1 & t.Centroid(i,3) >= coord3-1 & t.Centroid(i,3) <= coord3+1
total_Area = total_Area + t.Area(i);
end
end
end
function total_Area = sum_area_objects_range(t,coord1_min,coord1_max,coord2_min,coord2_max,coord3_min,coord3_max)
total_Area = 0;
for i = 1:height(t)
if t.Centroid(i,1) >= coord1_min & t.Centroid(i,1) <= coord1_max & t.Centroid(i,2) >= coord2_min & t.Centroid(i,2) <= coord2_max & t.Centroid(i,3) >= coord3_min & t.Centroid(i,3) <= coord3_max
total_Area = total_Area + t.Area(i);
end
end
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!