what is wrong with my code

조회 수: 13 (최근 30일)
Alexandra Huff
Alexandra Huff 2016년 8월 5일
댓글: Walter Roberson 2016년 8월 5일
Hi. so i am trying to solve this quetion: Write a function called flip_it that has one input argument, a row vector v, and one output argument, a row vector w that is of the same length as v. The vector w contains all the elements of v, but in the exact opposite order. For example, is v is equal to [1 2 3] then w must be equal to [3 2 1]. You are not allowed to use the built-in function flip.
my code for it is:
function w = flip_it(v)
v = [1 2 3 4 5];
w = v(end:-1:1);
For some reason I am getting an error. How do I fix it?

채택된 답변

Image Analyst
Image Analyst 2016년 8월 5일
You're redefining v inside your function. So whatever you pass in gets ignored. Don't do that. Delete the line
v = [1 2 3 4 5];
and it should work fine.
  댓글 수: 1
Alexandra Huff
Alexandra Huff 2016년 8월 5일
that fixed it! thanks so much

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 8월 5일
  댓글 수: 2
Alexandra Huff
Alexandra Huff 2016년 8월 5일
hi, I do have the function called flip_it. so I am not sure what is wrong
Walter Roberson
Walter Roberson 2016년 8월 5일
You do not show a function named flip_it. What you show is some code that starts by calling a function named flip_it

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

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by