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

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

    You can use the function tabulate. See this example with your vector.

    vector = [ 2 2 2 2 1 1 1 2 2 1 1 1 2 2 2 2 1 2 ];
    tabulate(vector);
      Value    Count   Percent
          1        7     38.89%
          2       11     61.11%
    

    If you need it in percent order, do:

    t = tabulate(vector);
    t = sortrows(t, 3)
    

提交回复
热议问题