For Loop to calculate Convhull & Polyarea
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
DGM
le 31 Oct 2022
Modifié(e) : DGM
le 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 commentaires
DGM
le 31 Oct 2022
Modifié(e) : DGM
le 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
Voir également
Catégories
En savoir plus sur Computational Geometry dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!