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 Center および File ExchangeInterpolation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by