For Loop to calculate Convhull & Polyarea

1 view (last 30 days)
Tania
Tania on 31 Oct 2022
Commented: Tania on 4 Nov 2022
Hi
I’m trying to create a for loop to calculate the convhull and polyarea for each row of variables within an array.
I have two separate arrays one containing X data points and one containing Y data points (data attached). They are both the same size being 6135 x 7 array.
I need to create a for loop to calculate the convhull first and then the polyarea of each row of the array.
So far, I have the following, but get an error message saying “Error computing the convex hull. Not enough unique points specified”.
I’m clearly missing something. If anyone can help that would be greatly appreciated.
Xnum_rows=size(Xdata,1);
Ynum_rows=size(YData,1);
for i = 1:1:Xnum_rows %increments by 1
for j = 1:1:Ynum_rows %increments by 1
CHull=convhull((Xdata(i)), (YData(j)));
SA=polyarea(Xdata(CHull),YData(CHull));
end
end
Output = SA;

Answers (1)

DGM
DGM on 31 Oct 2022
Edited: DGM on 31 Oct 2022
You're trying to find the convex hull of a single point instead of the whole row.
CHull = convhull(Xdata(i,:), YData(j,:));
  3 Comments
DGM
DGM on 31 Oct 2022
Edited: DGM on 31 Oct 2022
I missed that. More or less, yes. You're overwriting your outputs each time. That said, the output of convhull() will not necessarily be the same length each time. If you need to store all the index lists from convhull(), you'll need to store them in something like a cell array. Since the output of polyarea() is (in this case) scalar, you could just store that in a plain numeric matrix.
If all you need to keep is SA:
Xdata = rand(5,10);
YData = rand(6,10);
Xnum_rows = size(Xdata,1);
Ynum_rows = size(YData,1);
SA = zeros(Ynum_rows,Xnum_rows); % preallocate
for i = 1:1:Xnum_rows %increments by 1
for j = 1:1:Ynum_rows %increments by 1
CHull = convhull((Xdata(i,:)), (YData(j,:)));
SA(j,i) = polyarea(Xdata(CHull),YData(CHull));
end
end
SA
SA = 6×5
0.0946 0.1052 0.0713 0.0600 0.1483 0.1595 0.0076 0.0871 0.2189 0.0303 0.0234 0.0218 0.1075 0.0312 0.0246 0.0733 0.0019 0.0442 0.0981 0.0725 0.2079 0.0853 0.0353 0.1168 0.0342 0.1177 0.0502 0.0335 0.1625 0.0835
Tania
Tania on 4 Nov 2022
Hello
My outputs are not what I expected.
Basically I want to also store the conhull outputs for the X/Y data points. I understand that this will not be a 6135 x1 array, and will differ for each row. I've transposed this output so that I can then run the conhull for the combined x/y data point to get the polyarea at each timepoint (row in the case). This show lead me with a 6135 x1 array of the polyarea for the data.
I'm not sure if the loop is treating the X and Y data points as seperate entities, or if there there is an issue with my polyarea (SA) calculation.

Sign in to comment.

Categories

Find more on Computational Geometry in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!