How to sum up every other column of a matrix?
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Dyuman Joshi
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!