matrix or array composed by zero-values

조회 수: 4 (최근 30일)
Francesco
Francesco 2014년 7월 15일
댓글: Matz Johansson Bergström 2014년 7월 15일
Hi, is it present a native matlab command able to verify if a matrix, or an array, given as input is only composed by zero-values? Often i use the find function with the condition matrix == 0 and then i check if the results of find function is equal to the length of the input matrix or array. Thank you!

채택된 답변

Roger Stafford
Roger Stafford 2014년 7월 15일
If M is the matrix or array, do:
all(M(:)==0)

추가 답변 (1개)

David Sanchez
David Sanchez 2014년 7월 15일
try with
any(all(M))
any(all(M)) = 0 when every element of M equals 0
  댓글 수: 1
Matz Johansson Bergström
Matz Johansson Bergström 2014년 7월 15일
Don't you mean all(all(M)) for a 2D matrix M?
Because
M=zeros(3); M(3,3)=1; any(all(M))
and
M=zeros(3); any(all(M))
gives the same answer, which is 0.
all returns 1 if all elements of a vector is non-zero. 'Any' returns 1 if any element of a vector is non-zero.
any( all(M) ) will return 1 if at least one of the columns of M is non-zero and 0 otherwise.

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by