Write a MATLAB program that will compute a weighted average of an array of data.
ID: 3638698 • Letter: W
Question
Write a MATLAB program that will compute a weighted average of an array of data. The program should take a row vector of weights
w = [wn,wn-1,...,w2,w1] and a row vector of data
x = [xn , xn-1 ,..., x2 , x1] and compute a weighted average according to the formula:
wa = [xn*wn + xn-1*wn-1 +...+x2 *w2 +x1 *w1)/n.Your program should work for any length of w and x as long as the lengths are the same.
The weights and data will come from a MATLAB data file (.mat file) and will be named w and x respectively. The data can be loaded into the workspace by your program by including the line load project3.mat; after your clear, clc, and close all commands.
Do not use MATLAB's array operations or MATLAB’s built in functions sum or mean. You should use iteration to solve this.
A sample program run for the data w = [1.0, 1.2, 0.8, 1.0] and x = [75, 80, 65, 78] should result in a weighted average of 75.25.
Explanation / Answer
sum = 0; for i=1:end for j=1:end sum = sum + x[i]*w[j] end end sum = sum/length(x);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.