MATLAB: How to find area of all possible polygons

MATLABpermutationpolyareapolygon

n = 5; % No. of control points
array = 1:1:n;
p=arrayfun(@(k) num2cell(nchoosek(array,k),2), 3:length(array), 'unif', 0);
p=cat(1,p{:});
p=cellfun(@(array) num2cell(perms(array),2),p,'unif',0);
p=cat(1,p{:});
x = [1 ; 2 ; 3 ; 4 ; 2 ]; % coordinate of control points

y = [8 ; 5 ; 8 ; 7 ; 6 ]; % coordinate of control points
for i = 1:length(p)
x_p{i,1} = x(p{i,1});
y_p{i,1} = y(p{i,1});
end
From the above code, I got two cells x_p & y_p which provides coordinates of all possible polygons. I want to find the array of areas of all polygons. The size of area array will be (300 * 1). Thanks in advance.

Best Answer

n = 5; % No. of control points
array = 1:1:n;
p=arrayfun(@(k) num2cell(nchoosek(array,k),2), 3:length(array), 'unif', 0);
p=cat(1,p{:});
p=cellfun(@(array) num2cell(perms(array),2),p,'unif',0);
p=cat(1,p{:});
x = [1 ; 2 ; 3 ; 4 ; 2 ]; % coordinate of control points

y = [8 ; 5 ; 8 ; 7 ; 6 ]; % coordinate of control points
% Vectorize the computing of areas (in S(:))
% by group of polygonals having the same length
np = cellfun('length',p);
[npu,~,J] = unique(np); % npu is logically [3,4,5]
s = zeros(size(p)); % allocate array of area result
for i = 1:length(npu) % == 3
b = J==i;
Pi = cat(1,p{b});
s(b) = polyarea(x(Pi),y(Pi),2);
end
% Many of them represents the same geometrical polygon
% The entire procedure seems redundant thus wasteful
allimax = find(s==max(s));
for j=1:length(allimax)
imax = allimax(j);
pmax = p{imax};
xpmax = x(pmax);
ypmax = y(pmax);
figure;
plot(xpmax([1:end 1]),ypmax([1:end 1]));
end