Multiple specific elements in an array
조회 수: 13 (최근 30일)
이전 댓글 표시
I have a matrix [a b c d e f g]. I want to sum [2*(a + b) + 3*(c+d) +4*(e+f)]. How do i go about doing this ?
댓글 수: 0
답변 (1개)
Bhaskar R
2020년 2월 20일
Assuming a, b,c, d, ..g are values in the vector say X
X = 1:7;
sum([2:4].*[sum(reshape(x(1:6), 2,3))]);
댓글 수: 5
Bhaskar R
2020년 2월 20일
No, I mean to say that you have asked for "sum [2*(a + b) + 3*(c+d) +4*(e+f)]" then asking for "Now i want to sum 2*.36 + 3*(.14 + .13 +.12 +.1 +.09) +4*(.04 +.02)". Each time we can't change the evaluation of the statement. If there are only less than 10 entries you can write complete statement without using any commands using matlab indexing.
for this
sum 2*.36 + 3*(.14 + .13 +.12 +.1 +.09) +4*(.04 +.02) =
res = sum([2*x(1), 3*x(2:6), 4*x(7:8)]);
For information on indexing https://in.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html
I think i am clear!
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!