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 次查看(过去 30 天)
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 个评论
dpb
dpb 2013-10-1
编辑:dpb 2013-10-1
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.

请先登录,再进行评论。

回答(1 个)

Roger Stafford
Roger Stafford 2013-10-1
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.

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by