For Loop to calculate Convhull & Polyarea
1 view (last 30 days)
Show older comments
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;
0 Comments
Answers (1)
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
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
See Also
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!