Return true if only one element in matrix is nonzero

조회 수: 4 (최근 30일)
Darlington Mensah
Darlington Mensah 2018년 5월 8일
답변: Guillaume 2018년 5월 8일
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

채택된 답변

Guillaume
Guillaume 2018년 5월 8일
nnz(yourmatrix) == 1

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by