Determining the number of occurrences of each unique element in a vector

前端 未结 4 1463
轮回少年
轮回少年 2020-11-27 06:33

How can I determine the relative frequency of a value in a MATLAB vector?

vector = [ 2 2 2 2 1 1 1 2 2 1 1 1 2 2 2 2 1 2 ];

What function w

4条回答
  •  清酒与你
    2020-11-27 07:05

    You can use unique in combination with histc to get the relative frequency.

    A=[1,2,3,1,2,4,2,1]; %#an example vector
    unqA=unique(A);
    

    This gives the unique elements as unqA=[1,2,3,4]. To get the number of occurances,

    countElA=histc(A,unqA); %# get the count of elements
    relFreq=countElA/numel(A);
    

    This gives countElA=[3,3,1,1] and relFreq=[0.3750, 0.3750, 0.1250, 0.1250], which is the relative frequency of the unique elements. This will work for both integers and floating points.

提交回复
热议问题