Adding rows with constraint

조회 수: 2 (최근 30일)
Bathrinath
Bathrinath 2014년 1월 9일
답변: David Sanchez 2014년 1월 9일
a=[10,11,8; 6,5,7; 4,6,5; 0,0,5]; output=[10,11,8; 16,16,15; 20,22,20; 0,0,25];
First row should be as it is, in the second row elements should be added, in the third row also elements should be added but in the final row if '0' is there that column should not be added column with out '0' has to be added.

채택된 답변

dpb
dpb 2014년 1월 9일
b=cumsum(a);
b(a==0)=0;
  댓글 수: 1
Bathrinath
Bathrinath 2014년 1월 9일
Thank you sir its working

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

추가 답변 (1개)

David Sanchez
David Sanchez 2014년 1월 9일
a=[
10,11,8;
6,5,7;
4,6,5;
0,0,5];
output = zeros(size(a));
output(1,:) = a(1,:);% First row should be as it is
output(2,:) = sum(a(1:2,:),1); % the second row elements should be added
output(3,:) = sum(a(1:3,:),1);
output(4,:) = sum(a(1:4,:),1).*(a(4,:)~=0);% final row if '0' is there that column should not be added column with out '0' has to be added.
>> output
output =
10 11 8
16 16 15
20 22 20
0 0 25

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by