# How to explore off diagonal elements in matrices without avoiding the for loop?

24 vues (au cours des 30 derniers jours)
Zeynab Mousavikhamene le 15 Avr 2020
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 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
James Tursa le 15 Avr 2020
Square matrices? Just the nearest off-diagonals?

Connectez-vous pour commenter.

### Réponses (2)

Peng Li le 15 Avr 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.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Vasilis Chasiotis le 8 Oct 2020
Modifié(e) : Vasilis Chasiotis le 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
...
end
So, you check all the off-diagonal elements of A without using a loop.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by