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

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 = [];

댓글을 달려면 로그인하십시오.

답변 (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

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?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2023년 10월 14일

댓글:

2023년 10월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by