How to reset the 'lower triangle' of a 3 dimentional matrix

1 回表示 (過去 30 日間)
JazzMusic
JazzMusic 2016 年 11 月 28 日
コメント済み: JazzMusic 2016 年 11 月 28 日
Hi,
I need to reset the 'lower triangle' of a 3 dimentional matrix. This means, that if the original matrix is:
C(:,:,1) = [1 2 3 ; 2 4 6 ; 3 6 9]
C(:,:,2) = [2 4 6 ; 4 8 12 ; 6 12 18]
C(:,:,3) = [3 6 9 ; 6 12 18 ; 9 18 27]
Then the resulting matrix should be:
C(:,:,1) = [1 2 3 ; 2 4 6 ; 3 6 9]
C(:,:,2) = [0 0 0 ; 4 8 12 ; 6 12 18]
C(:,:,3) = [0 0 0 ; 0 0 0 ; 9 18 27]
Any idea how such a thing csn be done? (My original 3 dim matrix is large)
Thanks!

採用された回答

Ryan Smith
Ryan Smith 2016 年 11 月 28 日
Brute force method:
D3 = length(C(1,1,:));
D2 = length(C(1,:,1));
D1 = length(C(:,1,1));
for i = 2:D1
for j = 1:i-1
for k = 1:i-1
C(i,j,k) = 0;
end
end
end
Above provides [1 2 3; 0 4 6; 0 0 9] ; [2 4 6; 4 8 12; 0 0 18]; [3 6 9; 6 12 18; 9 18 27], which I believe would be the 'true' lower triangle. Don't quote me on that. To get what you requested, via 'brute force':
b = zeros([1 length(C(1,:,1))]);
for k = 2:D3
for i = 1:k-1
C(i,:,k) = b;
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by