Clear Filters
Clear Filters

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

3 views (last 30 days)
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!

Accepted Answer

Ryan Smith
Ryan Smith on 28 Nov 2016
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

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!