MATLAB: Compute the count of elements in a cell array

cell arrays

I have a cell array named "catgUsers", which represents "n" users and the cluster id, he has…. from that cell array i need to count the total number of similar images he has in each cluster…
i used the below code
for i = 1 : length(users)
count = hist(x, unique(x));
catgCount{i} = count;
end

Best Answer

You have the problem that if there is only one unique value in a cell, then unique() of the content is a scalar, and when hist() is passed a scalar as its second argument, it always interprets that as being the number of bins to use instead of indicating the bin centers.
hist() interprets a vector passed as the second argument as being the bin centers.
I recommend you use histc() or histcounts() instead of hist() if you are wanting to count specific items.
I can see from your desired outputs that you do not want to be passing in unique() of the values as the bin locations: your output would be what you would expect for passing in 1:max(unique()) for the edges.
However, in past discussions you indicate that your entries could be negative or floating point numbers, and if that is still the case then using 1:max(unique()) would be a mistake.
You appear to still be confusing local symbols (counts only for items present in one particular cell) with global symbols (counts reflecting items that might be encountered in some cell. You need to use one of:
  • entirely local symbols, in which case [2;4] is properly coded as [1;1], one occurrence of each; or
  • entirely global symbols, in which you scan all of the cells to find all of the symbols that occur anywhere and count them specifically, without leaving "holes" for any value that might be "missing"; in this case, the counts that you use for arithmetic encoding should be the count over everything; or
  • some hybrid of these two, where you create a group of cells, find the symbols common to them, create a count table for those symbols, with the counts for arithmetic encoding being relative only to that group.
Your counts for arithmetic encoding should reflect all symbols either until the end of transmission or the next point of logically resetting. Broadly speaking, you typically want to reset each time the statistics of the source change significantly, as might be the case for a new chapter of a book.