count elements falling within certain thresholds in array in matlab?

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

    Why not simply do something like this?

    % Random data
    m1 = 100*rand(1000,1);
    
    %Count elements between 10 and 20
    m2 = m1(m1>10 & m1<=20);
    length(m2) %number of elements of m1 between 10 and 20
    

    You can then put things in a loop

    % Random data
    m1 = 100*rand(1000,1);
    
    nb_elements = zeros(10,1);
    
    for k=1:length(nb_elements)
        temp = m1(m1>(10*k-10) & m1<=(10*k));
        nb_elements(k) = length(temp);
    end 
    

    Then nb_elements contains your data with nb_elements(1) for the 0-10 range, nb_elements(2) for the 10-20 range, etc...

提交回复
热议问题