For Loop to calculate Convhull & Polyarea
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
DGM
2022-10-31
编辑:DGM
2022-10-31
You're trying to find the convex hull of a single point instead of the whole row.
CHull = convhull(Xdata(i,:), YData(j,:));
3 个评论
DGM
2022-10-31
编辑:DGM
2022-10-31
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Computational Geometry 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!