Multiple specific elements in an array

3 vues (au cours des 30 derniers jours)
rockstar49
rockstar49 le 20 Fév 2020
Commenté : rockstar49 le 20 Fév 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 ?

Réponses (1)

Bhaskar R
Bhaskar R le 20 Fév 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 commentaires
Bhaskar R
Bhaskar R le 20 Fév 2020
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)]);
I think i am clear!
rockstar49
rockstar49 le 20 Fév 2020
thank you! I did not know of this.
Best,
Zakir M

Connectez-vous pour commenter.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by