Matrix calculation, sum of two rows in each column

조회 수: 3 (최근 30일)
monmatlab
monmatlab 2016년 12월 20일
댓글: Andrei Bobrov 2016년 12월 20일
I have a matrix d and I want to calculate for every column the sum of each to numbers. The Result should be the result matrix
d= 2 3 7
6 7 8
9 1 5
4 3 5
6 7 8
2 3 6
result= 8 10 15
13 4 10
8 10 14
result=[];
[x,y]=size(d);
for y=1:length(d(:,y))
for x=1:2:length(d(x,:))
[x,y]=size(d);
res=d(x)+d(x+1);
result=[result res];
end
end
I just get a vector consisting of 5. What should I change in the code? Thank you!
  댓글 수: 1
Jos (10584)
Jos (10584) 2016년 12월 20일
I suggest you debug your code, tracing each step. the variables x and y may not behave as you expect.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 12월 20일
d= [ 2 3 7
6 7 8
9 1 5
4 3 5
6 7 8
2 3 6];
result = squeeze(sum(reshape(d',size(d,2),2,[]),2))';
  댓글 수: 4
monmatlab
monmatlab 2016년 12월 20일
편집: monmatlab 2016년 12월 20일
d1=d2:2:end,:).*3;
d(2:2:end,:)=d1
result = squeeze(sum(reshape(d',size(d,2),2,[]),2))';
I have managed to do it this way
Andrei Bobrov
Andrei Bobrov 2016년 12월 20일
for 2016b
result = squeeze(sum(reshape(d',size(d,2),2,[]).*[1,3],2))'

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by