MATLAB Answers

Multiple specific elements in an array

16 views (last 30 days)
Zakir Mahmood
Zakir Mahmood on 20 Feb 2020
Commented: Zakir Mahmood on 20 Feb 2020
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 ?

Answers (1)

Bhaskar R
Bhaskar R on 20 Feb 2020
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 Comments
Zakir Mahmood
Zakir Mahmood on 20 Feb 2020
thank you! I did not know of this.
Best,
Zakir M

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!