compute the average of a vector in matlab

To compute the average of a vector in Matlab, you can use the mean() function. Here's an example:

main.m
v = [1 2 3 4 5]; %define vector
avg_v = mean(v); %compute average
disp(avg_v); %print average
94 chars
4 lines

Output:

main.m
3
2 chars
2 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