How to sum up every other column of a matrix?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jake
le 10 Mar 2023
Réponse apportée : Dyuman Joshi
le 10 Mar 2023
Suppose I have a matrix A,
A=[1 2 3 5 1 2 3 5;3 4 6 7 1 2 3 5;5 6 8 9 1 2 3 5]
How can I sum up column(1) and column(3) to form one column, and column(5) and column(7) to form another column? Similarly with even columns, i.e. column(2) and (4) forms a new column, and the column(6) and (8) forms another. So the output would like like.
B = [4 4;9 4;13 4] % odd columns
C = [7 7;11 7; 15 7] % even columns
0 commentaires
Réponse acceptée
Dyuman Joshi
le 10 Mar 2023
%Data with more columns than example mentioned
A=randi(10,3,16)
%Pairing odd columns - (1,3) (5,7) (9,11) ...
B=A(:,1:4:end)+A(:,3:4:end)
%Pairing even columns - (2,4) (6,8) (10,12)
C=A(:,2:4:end)+A(:,4:4:end)
0 commentaires
Plus de réponses (0)
Voir également
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!