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
답변 (1개)
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!