For Loop to calculate Convhull & Polyarea
조회 수: 2 (최근 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!