To compute the average of a vector in Matlab, you can use the mean()
function. Here's an example:
main.m94 chars4 lines
Output:
main.m2 chars2 lines
In this example, we first define a vector v
with 5 elements. We then use the mean()
function to compute the average of v
and store it in the variable avg_v
. Finally, we print the value of avg_v
using the disp()
function.
Note that the mean()
function can also be used with matrices in Matlab to compute the mean across rows or columns, depending on the value of the optional dim
argument.
gistlibby LogSnag