Is there a more elegant replacement for this MATLAB loop?

徘徊边缘 提交于 2019-12-10 17:20:00

问题


I want to get better at vectorizing my loops in MATLAB. At the moment, I'm trying to count the occurrences of values in a list of ints. My code is similar to this:

list = [1 2 2 3 1 3 2 2 2 1 5];
occurrence_list = zeros(1,max(list));

for x=list
    occurrence_list(x) = occurrence_list(x) + 1;
end

Is there a simple vectorized replacement for that for loop? (Or is there a built in MATLAB function that I'm missing?) I'm doing this on pretty small data sets, so time isn't an issue. I just want to improve my MATLAB coding style.


回答1:


In addition to the HIST/HISTC functions, you can use the ACCUMARRAY to count occurrence (as well as a number of other aggregation operations)

counts = accumarray(list(:), 1)
%# same as: accumarray(list(:), ones(size(list(:))), [], @sum)

Another way is to use TABULATE from the Statistics Toolbox (returns value,count,frequency):

t = tabulate(list)
t =
            1            3       27.273
            2            5       45.455
            3            2       18.182
            4            0            0
            5            1       9.0909

Note that in cases where the values don't start at 1m or in case there's large gaps between the min and the max, you will get a lot of zeros in-between the counts. Instead use:

list = [3 11 12 12 13 11 13 12 12 12 11 15];
v = unique(list);
table = [v ; histc(list,v)]'

table =
     3     1
    11     3
    12     5
    13     2
    15     1

representing the unique values and their counts (this will only list values with at least one occurrence)




回答2:


You can use the hist function for that. Specify an output, and force the bins to be integers over the range of your array.

list = [1 2 2 3 1 3 2 2 2 1 5]; 
bins = min(list):max(list);
counts = hist(list,bins);



回答3:


So, this is basically a histogram. Off my memory - look for the HIST function.



来源:https://stackoverflow.com/questions/3427291/is-there-a-more-elegant-replacement-for-this-matlab-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!