Count number of values in matrix within given range

后端 未结 5 1733
小鲜肉
小鲜肉 2020-12-10 08:46

I have matrix

A=[2 3 4 5 6 7;
   7 6 5 4 3 2]

I want to count how many number of elements have a value greater than 3 and less than 6.

5条回答
  •  攒了一身酷
    2020-12-10 09:03

    You could use matlab for-loop and cycle through the values on your own. Pros is that any function can be specified (>2 & <5 ; >3 & <6; etc.), cons is that it's kind of heavy approach. Here's approximate code:

    count = 0;
    for i=1:length(A)
      element = A(i);
      if (element > 2 && element < 5) 
        count = count + 1;
      end
    end
    

提交回复
热议问题