I am supposed to implement the following function without loops. function L = polyline(u,v) % u and v are column (n+1)-vectors with u(1) = u(n+1) and v(1) = v(n+1). % L is the perimeter of the polygon with veritices (u(1),v(1)​),...(u(n)​,v(n))

1 view (last 30 days)
Kevin Junior
Kevin Junior on 1 Oct 2013
Commented: Kevin Junior on 2 Oct 2013
I am supposed to use vectorized arithmetic. of z is a length-5 vector then alpha = sum(abs(z(1:4)-z(2:5))). Here is what I got so far.
function L = Polyline(u,v)
u = [1;1] % initialize vector u
v = [1;1] % initialize vector v
if length(u)== 0 && length(v) == 0
L == 0 % Perimeter of the polygon
else if length(u) > 0 && length(v) > 0
alpha = sum(abs(u(1:n)-v(2:n+1)))
end
end
L = alpha;
  1 Comment
dpb
dpb on 1 Oct 2013
Ask the question in the body, not the title...I can't read the question well enough w/o too much effort to try to decipher it.

Sign in to comment.

Answers (1)

Roger Stafford
Roger Stafford on 1 Oct 2013
1) The first two lines of code destroy your input vectors, u and v. Also there is nothing accomplished by them. This would prevent you from ever obtaining an answer. You need u and v very badly for your subsequent calculations.
2) The formula for length is incorrect. The n-th segment has a length equal to the square root of the sum of the squares of the differences between the x-coordinates of the two end vertices and that of their y-coordinates.
3) If you write your code properly you won't need to make a special case for empty u and v.

Tags

Community Treasure Hunt

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

Start Hunting!