Checking if a matrix has all its elements filled with one or not?

4 vues (au cours des 30 derniers jours)
Abhishek
Abhishek le 29 Avr 2011
Suppose I have a matrix like x = [1 1 1 1 1 1]. Now I have to write a if condition, where I have to check whether "x" contains all its elements as ones or not? How to do that. I searched in matlab's help, but couldn't find any direct "command" like to check such an existence.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 29 Avr 2011
  3 commentaires
Abhishek
Abhishek le 29 Avr 2011
@andrei: all(x==1) returns a value 1 even if I have all zeroes as my elements. x = [0 0 0 0 0 0] which should not be the case, right?
Walter Roberson
Walter Roberson le 29 Avr 2011
>> x = [0 0 0 0 0 0]
x =
0 0 0 0 0 0
>> all(x==1)
ans =
0
So if you are getting a 1 result from that, something is wrong.
The number of non-zero elements is nnz(x)
The original answer of all(x) does not match your problem conditions, as it would return true for the situation in which all of the x are non-zero. Your variation of all(x==1) is correct for vectors. For general matrices, all(x(:)==1)
A variation would be isequal(x, ones(size(x),class(x))

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Genomics and Next Generation Sequencing 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