count elements falling within certain thresholds in array in matlab?

后端 未结 6 1653
无人及你
无人及你 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:16

    Or you can create logical arrays and then use element-wise multiplication. Don't know how fast this is though and it might use a lot of memory for large arrays.

    Something like this

    A(find((A>0.2 .* (A<0.8)) ==1))
    

    Generate values

    A= rand(5)

    A =

    0.414906   0.350930   0.057642   0.650775   0.525488
    0.573207   0.763477   0.120935   0.041357   0.900946
    0.333857   0.241653   0.421551   0.737704   0.162307
    0.517501   0.491623   0.016663   0.016396   0.254099
    0.158867   0.098630   0.198298   0.223716   0.136054
    

    Find the intersection where the values > 0.8 and < 0.2. This will give you two logical arrays and the values where A>0.2 and A<0.8 will be =1 after element-wise multiplication.

    find((A>0.2 .* (A<0.8)) ==1)
    

    Then apply those indices to A

    A(find((A>0.2 .* (A<0.8)) ==1))
    
    ans =
    
       0.41491
       0.57321
       0.33386
       0.51750
       0.35093
       0.76348
       0.24165
       0.49162
       0.42155
       0.65077
       0.73770
       0.22372
       0.52549
       0.90095
       0.25410
    

提交回复
热议问题