count elements falling within certain thresholds in array in matlab?

后端 未结 6 1740
无人及你
无人及你 2021-01-28 07:56

I have a huge vector. I have to count values falling within certain ranges. the ranges are like 0-10, 10-20 etc. I have to count the number of values which fall in certain rang

6条回答
  •  长发绾君心
    2021-01-28 08:35

    You can use logical indexing. Observe:

    >> x = randi(40, 1, 10) - 20
    x =
      -2    17   -12    -9   -14   -14    15     4     2   -14
    >> x2 = x(0 < x & x < 10)
    x2 =
       4     2
    >> length(x2)
    ans =
        2
    

    and the same done in one step:

    >> length(x(0 < x & x < 10))
    ans =
        2
    

提交回复
热议问题