How to reset the 'lower triangle' of a 3 dimentional matrix
3 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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 Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!