How to sum up every other column of a matrix?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jake
am 10 Mär. 2023
Beantwortet: Dyuman Joshi
am 10 Mär. 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 Kommentare
Akzeptierte Antwort
Dyuman Joshi
am 10 Mär. 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!