How to sum up every other column of a matrix?
조회 수: 6 (최근 30일)
이전 댓글 표시
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
채택된 답변
Dyuman Joshi
2023년 3월 10일
%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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!