extracting non-zero elements in a matrix or an array
55 次查看(过去 30 天)
显示 更早的评论
Hello, I have two questions. I would be so glad if you can please help me.
1. How can I extract the number of non-zero elements in a matrix or in an array?
2. I have 3x3 (3d) array which is a cell. How can I add the diagonal elements or off-diagonal elements in a cell?
0 个评论
采纳的回答
Paulo Silva
2011-4-18
a=[1 0 2;3 4 5;0 7 8]
a(a~=0) %get all the non zero values
diag(a) %elements on the diagonal
s1=sum(diag(a)) %sum of the elements on the diagonal
sum(sum(a))-s1 %sum of non-diagonal elements
3 个评论
Paulo Silva
2011-4-18
use the cellfun function or make a loop to apply the code to every cell element.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!