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 Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by