How to sum up multiple vectors, element by element, to recieve one final value?
조회 수: 52 (최근 30일)
이전 댓글 표시
I am a beginner to MATLAB and I'm probably trying to run before I can walk here, however, I need to calculate this equation below and I have found no explanations online:
![Equ.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/221719/Equ.jpeg)
Where, WP and Op are vectors with 8760 iterations each.
I want to conduct this equation for each iteration of h (i.e. WP(1)*Op(1)/Op(1)) and summate each value to find a single value for the whole calculation (Ex).
I've been down the for loop route but not fully understanding how I can link it to my problem.
Any help would be amazing. Thanks!
댓글 수: 0
채택된 답변
Kevin Phung
2019년 5월 29일
편집: Kevin Phung
2019년 5월 29일
example:
W = [1 2 3 4 5];
O = [1 2 3 4 5];
%do the numerator first:
E = W.*O; %element wise product
%sum up the products, and divide by the sum of all elements in O:
E = sum(E)/sum(O); % E should now be a scalar -- your answer
let me know if this helps.
댓글 수: 0
추가 답변 (1개)
James Tursa
2019년 5월 29일
편집: James Tursa
2019년 5월 29일
The numerator is just a simple dot product between two vectors, and the denominator is just a simple sum. Using the fact that a dot product between two vectors can be accomplished with the matrix multiply operator, you can just do a (row vector) * (column vector) operation on top and a sum on the bottom. E.g.,
If both are row vectors, then
E = WP * Op.' / sum(Op);
If both are column vectors, then
E = WP.' * Op / sum(Op);
Note that the .' operation is a non-conjugate transpose.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!