memnus: A stylized galaxy image, with the quote "Eternity lies ahead of us - and behind. Have you drunk your fill?" (Default)
Brian ([personal profile] memnus) wrote2004-06-18 09:08 am

Late, but as promised

I promised a matlab puzzle.

So I'm trying to get the probability of an event from a multinomial distribution. I currently have a function that takes in a vector x and returns the number of permutations of that many elements:
d = factorial(sum(x));
for y = x
    d = d / factorial(y);
end
return d

What I'd like to be able to do is accept a 2-d matrix, and have this function act on each row and return a column vector, without using any for loops. I can write factorial any of the following ways for a single value:
prod(1:x)
prod(cumsum(ones(1, x)))

But again, those don't generalize so well. A factorial that generalizes to act on each element of an array would be spiffy.

Edit: The exact function I'm looking for exists in Matlab 7. In fact, the factorial() function in matlab 7 works exactly how I want it to. I'm using matlab 6.5. If anyone out there actually has matlab 7, and can open their factorial.m and tell me how it works, I'd be much obliged.

click

[identity profile] squirrelloid.livejournal.com 2004-06-18 01:42 pm (UTC)(link)
oh, and thats the hard part. obviously the rest of the computation is trivial once you have that.