What is the purpose of this line in MATLAB's "factorial.m" function?

조회 수: 1 (최근 30일)
Blake
Blake 2012년 6월 17일
EDIT: I have removed the rest of the FACTORIAL function in this post as per Jan's suggestion. To explain myself, I did consider whether posting a MATLAB function to Answers is copyright infringement. I didn't think so because the code is in MATLAB's language...and I didn't think it would be that helpful for a nefarious party to see a function in MATLAB like this. I may be mistaken, though, and will refrain from posting any MATLAB code from now on. But my question still stands...
I am confused about a line in MATLAB's FACTORIAL function. If you type "open factorial" in the command window and check (somewhere around) line 20, it says:
m = max([1; n(:)]);
n(:) is an array of real, nonnegative integers. 1 is appended to n(:) and that is passed to the MAX function...the only reason I can see for that 1 is to prevent MAX from returning 0 if n(:) consists entirely of 0's. But the rest of the code doesn't seem to require the MAX to return 1 instead of 0. What could be the purpose of this appended 1? I ask because I'm just learning MATLAB and may be mistaken in thinking this extra 1 is unnecessary.
  댓글 수: 2
Jan
Jan 2012년 6월 17일
It is surprising that you do not hesitate to publish the code of a toolbox function. Please read this again:
edit(fullfile(matlabroot, 'license.txt')), Software License Agreement, section 4.5.
Then I suggest to remove the source code fast.
Blake
Blake 2012년 6월 18일
I have removed the code. I did consider whether posting a MATLAB function to Answers is copyright infringement. I didn't think so because the code is in MATLAB's language...and I didn't think it would be that helpful for a nefarious party to see a function in MATLAB like this. I may be mistaken, though, and will refrain from posting any MATLAB code from now on. But my question still stands, do you know whether that 1 is necessary and why?

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

채택된 답변

Blake
Blake 2012년 6월 19일
I believe the 1 is a remnant of the code before the author manually included an exception for 0 and 1 factorial. The author may have forgotten to remove it after changing a previous part, but since this doesn't cause an error, he may not have caught it (or left it in because it makes no difference).

추가 답변 (1개)

Geoff
Geoff 2012년 6월 18일
I would hazard that it is there to handle the special case of computing zero-factorial, which of course is 1.
  댓글 수: 1
Blake
Blake 2012년 6월 19일
This was my first guess as well. But zero factorial is accounted for in the next line without this extra 1. The use of "m" in the next line is "cumprod(2:m)". 2:m returns an empty matrix whether m is 1 or 0.
I believe the 1 is a remnant of the code before the author manually included an exception for 0 and 1 factorial. The author may have forgotten to remove it after changing a previous part, but since this doesn't cause an error, he may not have caught it (or left it in because it makes no difference).

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by