Check if all vector elements have same sign

11 次查看(过去 30 天)
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?

采纳的回答

Stephen23
Stephen23 2018-5-31
编辑:Stephen23 2018-5-31
true if all non-zero elements of V have the same sign:
~any(diff(sign(V(V~=0))))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Deep Learning Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by