One way to create a vector containing all the factorial numbers from 1 to 13 is by using the factorial
function in Matlab. Here's the code that creates such a vector using a loop:
main.m195 chars11 lines
However, since Matlab supports vectorization, we can achieve the same result more concisely using a single line of code:
main.m138 chars6 lines
This code creates a vector of integers from 1 to 13 using the colon operator (1:13
), then applies the factorial
function to each element of the vector using vectorization, and finally assigns the resulting vector to the factorials
variable. The resulting factorials
vector contains all the factorial numbers from 1 to 13.
Note that the factorial
function returns doubles, not integers, so the factorials
vector will contain double-precision numbers. If you want to convert the vector to integers, you can use the int64
function:
main.m126 chars6 lines
gistlibby LogSnag