How can I make a function that has an input that cake take handle both scalar and array values?

function [format,area] = polygonArea(numSides,sideLengths)
L = length(sideLengths);
RightTriangleArea = (L(1)*L(2))/2;
areaRectangle = sideLengths(1)*sideLengths(2);
p = sideLengths*numSides;
a = sideLengths/(2*tan(180/numSides));
regularPolygonArea = (p.*a)/2;
if numSides <3|| numSides <0
disp('invalid 1');
elseif numSides ==3 && L(1) ~= L(2)
area = RightTriangleArea;
format = true;
disp('valid right');
elseif numSides ==4 && L(1) ~= L(2)
area = areaRectangle;
format = true;
disp('valid rectangle');
elseif numSides >4 && size(sideLengths)== 0
area = regularPolygonArea;
format = true;
disp('valid polygon');
else
format = false;
area = false;
disp('invalid 2');
end

4 Comments

Which inputs do you want to be able to vectorize over?
If you want to vectorize over sideLengths, and if the number of sides can be different for each polygon, then do you want to use a cell array of side lengths, or do you want to pad the shorter sides with NaN, or ... ?
How do you want to handle the possibility that some polygons might be valid but others might not be? For example do you want to create an additional output that has the per-shape message in them?
By the way: if numSides is negative then numSides will be <3 so you do not need to test for both < 3 and < 0
For my project I have to write a function that takes in inputs that can describe polygons with different numbers of sides. And the function has to calculate the areas of the polygons. I want the input "sideLengths" to be able to take both scalar and array values and I need to my code to know if sideLength is a scalar and what to do with it. I can only put in array values into sideLengths.I don't how to make my code use the regularPolygonArea formula when sideLengths is scalar and not an array.
You could normally deduce numSides by looking at the size of the passed sideLengths .
However, it would be reasonable to allow a scalar side length to be passed in, along with the number of sides, with the interpretation to be that there are to be that many sides of equal length all of which are equal to the scalar.
See by the way isscalar -- but watch out also for isempty
I don't understand your statement of "I want the input sideLengths to be able to take both scalar and array values". Can you give examples for 4 cases, namely a Triangle, a Square, a Rectangle, a Trapezoid? Ignore other polygons at the moment.
Note: we can use Heron's formula to compute the area of a Triangle. No issue with that.
Case 1: Triangle (each side can have different length) -- general case
numSides = 3;
sideLengths = [];
Case 2: Square (all sides have the same length)
numSides = 4;
sideLengths = [];
Case 3: Rectangle (two sides have the same lengths)
numSides = 4;
sideLengths = [];
Case 4: Trapezoid (each side can have different length) -- general case
numSides = 4;
sideLengths = [];

Sign in to comment.

Answers (1)

Maybe you want to display the result like this?
numSides = 4;
sideLengths = [1, 2];
polygonArea(numSides, sideLengths)
valid rectangle
ans = logical
1
function [format,area] = polygonArea(numSides,sideLengths)
L = sideLengths;
RightTriangleArea = (L(1)*L(2))/2;
areaRectangle = sideLengths(1)*sideLengths(2);
p = sideLengths*numSides;
a = sideLengths/(2*tan(180/numSides));
regularPolygonArea = (p.*a)/2;
if numSides <3|| numSides <0
disp('invalid 1');
elseif numSides ==3 && L(1) ~= L(2)
area = RightTriangleArea;
format = true;
disp('valid right');
elseif numSides ==4 && L(1) ~= L(2)
area = areaRectangle;
format = true;
disp('valid rectangle');
elseif numSides >4 && size(sideLengths)== 0
area = regularPolygonArea;
format = true;
disp('valid polygon');
else
format = false;
area = false;
disp('invalid 2');
end
end

2 Comments

I got this error "Index exceeds the number of array elements. Index must not exceed 1."
Where do you get the error?
What are the input values you have provided? And have you used the same exact code @Sam has provided above?

Sign in to comment.

Categories

Products

Release

R2023b

Asked:

on 14 Oct 2023

Commented:

on 16 Oct 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!