How to check if all of the elements in an array are the same, in matlab?

后端 未结 4 1966
粉色の甜心
粉色の甜心 2020-12-09 02:07

There must be a simple matlab way of doing this. I have a row vector and I want to check if all of the elements are equal. Brute forcing this in a loop is easy, but lookin

相关标签:
4条回答
  • 2020-12-09 02:14

    I think it can be as simple as

    if all(v == v(1))
    

    Another method would be

    if range(v) == 0
    
    0 讨论(0)
  • 2020-12-09 02:20

    Another solution:

    sum(abs(diff(v))) == 0
    
    0 讨论(0)
  • 2020-12-09 02:33

    Another solution:

    numel(unique(v))==1
    
    0 讨论(0)
  • 2020-12-09 02:36

    How about: max(v) == min(v)? :-)

    0 讨论(0)
提交回复
热议问题