Return true if only one element in matrix is nonzero

2 Ansichten (letzte 30 Tage)
Darlington Mensah
Darlington Mensah am 8 Mai 2018
Beantwortet: Guillaume am 8 Mai 2018
How do I find out if my matrix contains only one non-zero element and return true, if not false.
Such that
0 0 0
0 0 0
0 0 0
should return FALSE(all element are zero)
0 0 0
0 0 1
0 0 1
should return FALSE(more than one element is non-zero)
And finally;
0 0 0
0 0 0
0 0 1
should return TRUE
I could use a for loop but i was wondering if there is any function that can do that

Akzeptierte Antwort

Guillaume
Guillaume am 8 Mai 2018
nnz(yourmatrix) == 1

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by