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

前端 未结 4 1496
轮回少年
轮回少年 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 06:42

    For the most general case where you have a vector of floating point values, you can use the functions UNIQUE and ACCUMARRAY:

    [uniqueValues,~,uniqueIndex] = unique(vector);
    frequency = accumarray(uniqueIndex(:),1)./numel(vector);
    

提交回复
热议问题