How to write a function to test for positive or negative numbers

조회 수: 19 (최근 30일)
Jonathan Toh
Jonathan Toh 2017년 10월 4일
편집: DGM 2023년 8월 22일
Hi! I need to write a function that takes in numerical data as input argument and prints a message to the command window stating if the number is positive, negative or zero.

채택된 답변

Jan
Jan 2017년 10월 4일
편집: Jan 2017년 10월 4일
This sounds like a homework. Therefore the forum will not post a solution, because this is your homework. Post, what you have tried so far and ask a specific question. Do you know how to write a function? Search in the Getting Started chapters of the documentation, if not. Do you know the if command? Try the examples from the docs:
doc if
The sign command might help also, e.g. by switch sign(x). Then you get 3 cases: -1, 0 and +1 ans answer. See
doc switch
Printing a message to the command window works with:
fprintf('hello\n')
or
disp('This is the message')
Try it and come back, if you have a specific question.
Good luck!

추가 답변 (3개)

Rik
Rik 2017년 10월 4일
You can find guidelines for posting homework on this forum here.
Have a look at the function sign, or solve this with if, elseif and else.
For printing text to the terminal, you can use disp or fprintf.
If you have trouble using one of these functions, you can open the documentation with doc FunctionName

Yenchia Feng
Yenchia Feng 2020년 12월 30일
Try
doc sign
Hope this helps!

suvam
suvam 2023년 8월 22일
check whether an alphabet is vowel or consonant
  댓글 수: 1
DGM
DGM 2023년 8월 22일
편집: DGM 2023년 8월 22일
This is not an answer to the question about positive an negative numbers. Why would you hide a question in an old unrelated thread unless you want nobody to answer it?
If you have a question, ask a question. Provide enough specific information about the task so that others know what your requirements are. This is relevant, because the question you've asked requests a misrepresentation of what vowels and consonants are. It really only warrants the naive answer. If you want something other than a naive, generally wrong answer, the problem isn't simple anymore, and you need to be more specific about what you're calling a "vowel".
See also PLL's comment on that answer.
... also, nobody said we're talking about english.

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

카테고리

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