How to explore off diagonal elements in matrices without avoiding the for loop?
59 views (last 30 days)
Peng Li on 15 Apr 2020
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 on 8 Oct 2020
Edited: Vasilis Chasiotis on 8 Oct 2020
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
So, you check all the off-diagonal elements of A without using a loop.