i am trying to write a function.

2 次查看(过去 30 天)
for example i want to calculate the area of a triangle. so i will use the formula 1/2(b*h). and i also want to calculate volume of a triangle and i will use 1/2(b*h*l). so when i am passing the base(b) and the height(h), i want it to give me the area, and when i am passing the base(b), height(h) and length(l) i want it to give me the volume. i want to use function for my input arguments. How can i do it?
  5 个评论
John D'Errico
John D'Errico 2018-1-31
编辑:John D'Errico 2018-1-31
Why do we care that you have finished your homework? Answers is here to help others too. They might have been able to learn from your question.
You never should have posted the question in the first place if you were attempting to cheat on your homework. And if your teacher would have been unhappy at seeing the question, then you were indeed cheating.
And, yes, I do remember that you said your teacher would kill you if they saw your question posted here. Deleting the comment does not delete it from my memory.
Personally, I do hope that your teacher happens to see this question, and then starts asking questions of you as to what the question was. Anyway, they can surely recognize what you asked, merely by reading the answers. So my fervent hope is that you will sill be unmasked as a person who is quite willing to cheat on their work. Sorry, but eventually your actions will catch up to you unless you start to learn personal responsibility.
As I said before, all you do is ensure that we will avoid answering further questions by you, or close them when we recognize them for what they are.
Jan
Jan 2018-1-31
编辑:Jan 2018-1-31
@rihan: Your question can be found in Google's cache, even if you delete it here:
for example i want to calculate the area of a triangle. so i will
use the formula 1/2(b*h). and i also want to calculate volume of
a triangle and i will use 1/2(b*h*l). so when i am passing the
base(b) and the height(h), i want it to give me the area, and
when i am passing the base(b), height(h) and length(l) i want it
to give me the volume. i want to use function for my input
arguments. How can i do it?
It is no cheating, if you ask for hints and not for a solution. Why should your teachers care as long as you do not try submit a solution written by someone else? Homework questions are welcome in this forum, as long as they are explicitly marked as homework and contain a specific question. Posting the current code and asking for assistance is not cheating. See https://www.mathworks.com/matlabcentral/answers/8626-how-do-i-get-help-on-homework-questions-on-matlab-answers.
But you have deleted the contents of several of your questions now. This is not a fair usage of the forum. John is right: The members of the forum will avoid answering. And they are smart enough to recognize, that you change your account name repeatedly.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2018-1-31
A small example code:
function r = Calculate(a, b, c)
switch nargin
case 2
disp(a)
disp(b)
r = a * b;
case 3
disp(a)
disp(b)
disp(c)
r = a * b + c;
otherwise
error('2 or 2 inputs required');
end
If you do not know how many inputs the function will get, use varargin. See
doc nargin
doc varargin

更多回答(1 个)

John D'Errico
John D'Errico 2018-1-30
编辑:John D'Errico 2018-1-30
Learn to use nargin, so you can test to see how many arguments were provided. Then you can put a branch in to do the calculation as you want.
That is, you can write a function to have three arguments, but if only two arguments were provided when you call it, MATLAB assumes it is the first two arguments. So nargin allows you to differentiate in how the function was called.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by