factorial function won't take a cell argument independently of it's class (double, integer, etc)
Show older comments
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.
Accepted Answer
More Answers (2)
Walter Roberson
on 8 May 2016
1 vote
Could you confirm that you are calling factorial(bins{1}) ? If so what is class(bins{1})
Image Analyst
on 8 May 2016
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 Comments
John D'Errico
on 8 May 2016
Very often, somebody sees something like 1.000, and assumes that it is indeed the integer 1.
format short g
1.000000001
ans =
1
Just because it looks like an integer does not mean it is one.
Image Analyst
on 8 May 2016
Good point John - we see that a lot. If they are intended to be integers they can cast to integers with round() or int32().
ethan1987
on 8 May 2016
John D'Errico
on 8 May 2016
Edited: John D'Errico
on 8 May 2016
And what did class tell you? For example:
X = {{ 1 }};
class(X{1})
ans =
cell
factorial(X{1})
Error using factorial (line 20)
N must be an array of real non-negative integers.
X{1} above is a cell itself, despite the fact that it contains an integer. X{1} is NOT an integer.
ethan1987
on 8 May 2016
ethan1987
on 8 May 2016
ethan1987
on 8 May 2016
ethan1987
on 8 May 2016
John D'Errico
on 8 May 2016
Why not attach the array as it is? (Attach it to your next comment as a .mat file.) Then we can see the variable, read it into MATLAB ourselves, and then help you to resolve your problem.
ethan1987
on 8 May 2016
Walter Roberson
on 9 May 2016
Give the command
format long g
and then print out your values again.
Image Analyst
on 9 May 2016
s = load('bins.mat')
b = s.bins{1}
b =
Columns 1 through 5
9.35 10.05 10.75 11.45 12.15
Columns 6 through 10
12.85 13.55 14.25 14.95 15.65
Columns 11 through 15
16.35 17.05 17.75 18.45 19.15
Columns 16 through 20
19.85 20.55 21.25 21.95 22.65
Columns 21 through 25
23.35 24.05 24.75 25.45 26.15
Columns 26 through 30
26.85 27.55 28.25 28.95 29.65
Columns 31 through 35
30.35 31.05 31.75 32.45 33.15
Columns 36 through 40
33.85 34.55 35.25 35.95 36.65
Columns 41 through 45
37.35 38.05 38.75 39.45 40.15
Columns 46 through 50
40.85 41.55 42.25 42.95 43.65
Is there something in there that does not look like integers??? Why do you think 9.35 factorial is a valid thing to do?
Walter Roberson
on 9 May 2016
If you have the symbolic toolbox, then replace
factorial(bins{1})
with
gamma(bins{1}+1)
gamma is the generalization of factorial.
Note: gamma has a singularity at every negative integer.
ethan1987
on 9 May 2016
ethan1987
on 9 May 2016
Image Analyst
on 9 May 2016
This works fine for me:
factorial(round(b))
John D'Errico
on 9 May 2016
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
on 9 May 2016
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!