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

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

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.

More Answers (2)

Could you confirm that you are calling factorial(bins{1}) ? If so what is class(bins{1})
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

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.
Good point John - we see that a lot. If they are intended to be integers they can cast to integers with round() or int32().
But i said i checked the class with class function
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.
there are no negatives because the numbers are bins of an histogram and their positions are times (positive reals)....also i had the problem of them not being integer, used the round function to no solution and then tried int16 uint16 and the posted error occurs...bins{1} is double as i posted and factorial of a double do work...but i don't know what's happening, factorial(uint16(bins{1})) MUST work...uint are unsigned integers!
sorry, i must say that factorial(round(bins{1})) do work but i gives me a number*array where array have the first few elements 0.000000.... and the inverse of those is Inf ...and thats my problem. factorial(uint16(bins{1})) directly does not work at all
also format long gives 0.0000....and so the non zero digit is to far away i guess? is there someway to actually see it? to make sure it is really non zero
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.
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?
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.
Image analyst: I didn't use factorial on bins as it is . i repeatedly said that i tried to round them or convert into integers!...also factorial of fractionals do exists even though i'm not doing such thing
Nevermind, i realized that round(bins{1}) is not null and therefore is not the cause on the Inf in my script calculations, thx anyway...But i still can't understand why factorial(uint16(bins{1})) won't work...factorial(uint16([3.4 2.8])) wont work either...what's the problem there??
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?
oh, of course...i didn't get to that part and realized that, hehe... I'm using matlab 7.12 R2011a

Sign in to comment.

Categories

Asked:

on 8 May 2016

Edited:

on 9 May 2016

Community Treasure Hunt

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

Start Hunting!