Check if all vector elements have same sign

Is there an efficient (and easy) way to check if all elements of a vector have the same sign (not caring about zero values)?
I can think of applying sign(v) and checking if all elements is equal to the first one after having removed all elements where sign(v)==0, but this fells somewhat clumsy. Is there an easier or more efficient way?

 Respuesta aceptada

Stephen23
Stephen23 el 31 de Mayo de 2018
Editada: Stephen23 el 31 de Mayo de 2018
true if all non-zero elements of V have the same sign:
~any(diff(sign(V(V~=0))))

Más respuestas (0)

Categorías

Más información sobre Develop Apps Using App Designer en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 31 de Mayo de 2018

Comentada:

el 1 de Jun. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by