필터 지우기
필터 지우기

factorial function won't take a cell argument independently of it's class (double, integer, etc)

조회 수: 1 (최근 30일)
i can't calculate the factorial of a cell element (being it an array), though it is double (checked it with class funct.) and factorial doing fine with a double array... and neither it works using int16 or uint16 conversions on the cell element (called bins {1}) it simply states that "N must be a matrix of non-negative integers." and i do give it such thing.

채택된 답변

Stephen23
Stephen23 2016년 5월 9일
편집: Stephen23 2016년 5월 9일
This is because of your MATLAB version. Here using MATLAB 2010b:
>> factorial(uint16([3.4 2.8]))
??? Error using ==> factorial at 17
N must be a matrix of non-negative integers.
>> factorial(round([3.4 2.8]))
ans =
6 6
The second line of code actually throws an error for this case:
~isa(N,'double')
which you would have found by simply clicking on the error message link.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2016년 5월 8일
Could you confirm that you are calling factorial(bins{1}) ? If so what is class(bins{1})

Image Analyst
Image Analyst 2016년 5월 8일
This works just fine:
% Make a 1-by-10 vector of random integers in the range 1-9
dblBins = randi(9, 1,10)
% Stick into a 1 by 2 cell array.
bins = {dblBins, dblBins}
% Extract the contents of the first cell and take it's factorial
f = factorial(bins{1})
Chances are you have non-integers in there or negative numbers.
  댓글 수: 18
John D'Errico
John D'Errico 2016년 5월 9일
No problem in the least here.
factorial(uint16([3.4 2.8]))
ans =
6 6
Note that the result is a uint16 number. So it is trivial to generate an overflow.
factorial(uint16(9.3))
ans =
65535
And...
factorial(uint16(bins{1}))
ans =
Columns 1 through 21
65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535
Columns 22 through 42
65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535 65535
Columns 43 through 50
65535 65535 65535 65535 65535 65535 65535 65535
Perhaps you need to tell us which release of MATLAB you are trying this with?
ethan1987
ethan1987 2016년 5월 9일
oh, of course...i didn't get to that part and realized that, hehe... I'm using matlab 7.12 R2011a

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by