How to explore off diagonal elements in matrices without avoiding the for loop?
显示 更早的评论
I need to search if the value of off diagonal elements of many matrices are larger than a specific number. Was wondering if I can do that without using a loop.
1 个评论
James Tursa
2020-4-15
Square matrices? Just the nearest off-diagonals?
回答(2 个)
Peng Li
2020-4-15
0 个投票
Not sure if many matrices share the same size. Anyway if you store these matrices in a cell you prob have a chance using cellfun to accomplish this I think. You can use triu or tril to get all off diagonal elements.
Vasilis Chasiotis
2020-10-8
编辑:Vasilis Chasiotis
2020-10-8
A = randn(n,n); % a n-by-n matrix of random numbers
idx = eye(n,n); % the identity matrix of order n
if all( A(~idx) > K ) % check if all the off-diagonal elements of A are greater than K
...
end
So, you check all the off-diagonal elements of A without using a loop.
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!