How to sum two rows in a matrix?

5 vues (au cours des 30 derniers jours)
Moe
Moe le 26 Avr 2014
Commenté : Moe le 26 Avr 2014
Hi everyone,
Suppose I have a matrix:
a = [4 2 1;1 9 6;4 22 4;12 15 1;1 6 8;5 2 6];
I want to summing every two rows together and write in matrix b:
Such as:
b= [23; 58; 28]
details of calculation:
23 = a (1,:) + a (2,:)
58 = a (3,:) + a (4,:)
28 = a (5,:) + a (6,:)
Can anyone help me please?

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 26 Avr 2014
Modifié(e) : Azzi Abdelmalek le 26 Avr 2014
a = [4 2 1;1 9 6;4 22 4;12 15 1;1 6 8;5 2 6]
b=sum(reshape(a',2*size(a,2),[]))
  1 commentaire
Moe
Moe le 26 Avr 2014
Thank you Azzi, just one transpose was needed in the last.

Connectez-vous pour commenter.

Plus de réponses (1)

Roger Stafford
Roger Stafford le 26 Avr 2014
b = sum(reshape(a.',2*size(a,2),[]),1).'; % Assume an even no. of rows
  1 commentaire
Moe
Moe le 26 Avr 2014
Thanks a lot Roger!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements 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