here is the question :
function can return a vector as a result. Write a function vecout that will receive
one integer argument and will return a vector that increments from the value of
the input argument to its value plus 5, using the colon operator.
I got : function vector= vecout(integer)
start= integer;
end= integer + 5;
vector= start:end;
end
The answer display Empty matrix: 1-by-0 What should I fix this porblem

 채택된 답변

Mohammad Abouali
Mohammad Abouali 2014년 10월 26일
편집: Mohammad Abouali 2014년 10월 26일

1 개 추천

This is very tricky
You can not use end as a variable name. That is a reserved word.
SO pretty much your definition of the function:
function vector= vecout(integer)
start= integer;
end = integer + 5;
vector= start:end;
end
becomes:
function vector= vecout(integer)
start= integer;
end
everything after the first "end" is ignored so vector is never assigned anything; thus returning as an empty array.
Correct it this way
function vector=vecout(n)
vector=n:n+5;
end

댓글 수: 4

david Chan
david Chan 2014년 10월 26일
i can do this with + but cannot do this in - like function vector=vecout(n) vector=n:n-5; end
it still said Empty matrix: 1-by-0
Mohammad Abouali
Mohammad Abouali 2014년 10월 26일
편집: Mohammad Abouali 2014년 10월 26일
n:n-5 returns a null vector.
Because n-5 is definitely smaller than n so that generates nothing. If you want it to generate something it should be n:-1:n-5. Then it starts going down (-1 step) to reach n-5. With plus n:+1:n+5 but since +1 is the default step you can omit it and just type n:n+5. That's why one works and the other doesn't.
david Chan
david Chan 2014년 10월 26일
I get it thank you for your answer
Mohammad Abouali
Mohammad Abouali 2014년 10월 26일
You are welcome

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2014년 10월 26일

댓글:

2014년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by