You can write a function in MATLAB to compute the inner product of two vectors by multiplying the corresponding elements of the vectors and then adding up the products. Here is an example implementation of such a function:
main.m265 chars10 lines
This function takes two vectors u
and v
as input, and returns their inner product. The function first checks that u
and v
have the same size using the built-in function isequal
. If they are not the same size, the function will throw an error. Otherwise, the function multiplies corresponding elements of u
and v
using the element-wise multiplication operator .*
, and then sums the resulting products using the built-in function sum
. The resulting sum is returned as the output of the function.
gistlibby LogSnag