Need help creating Easy function

10 次查看(过去 30 天)
Ulrik
Ulrik 2014-10-26
回答: Matt Tearle 2014-10-27
Make a function that takes an integer, n, and returns n!=1¢2¢3¢4....¢n.
Help is appreciated
  2 个评论
Jan
Jan 2014-10-26
Please try to be more precise. Du you mean the product of the values 1 to n? Than the ¢ characters should be '*' instead?
If so, this sounds like an easy homework question. As usual we will not solve this for you, bcause you could not submit our solution without cheating. So better show us what you have tried so far and ask a specific question.
Ulrik
Ulrik 2014-10-27
It's probably easy as it's an assignment for an introductory course in matlab; however, I don't understand the problem.

请先登录,再进行评论。

回答(1 个)

Matt Tearle
Matt Tearle 2014-10-27
MATLAB Academy can help you learn the basics of MATLAB. Or the "Getting Started" section in the doc.
You'll need to learn how to write a basic function. For example, the classic "area of a circle" function looks like this in MATLAB:
function A = areaofcircle(r)
A = pi*r.*r;
(file saved as areaofcircle.m). You need to write a function that, like this one, takes a single input called n and returns a variable that holds the value of 1*2*3*...*n. There are various ways to do that. A simple one is to use a for-loop. A "better" way is to use a vector and the prod function.
There is a factorial function in MATLAB. You can check that your function behaves the same as it:
>> factorial(5)
ans =
120

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by