user30609
user30609

Reputation: 264

Is there a way in matlab to count the number of times each number appears across cells

I have a cell in MATLAB

In the following format...

a = {{1,2,3},{1,2,3,5},{1,2,3},{1,2},{5}}

Is it possible to count the number of times each time each number appears between 1 and 5

For example, 1 appears 4 times, whilst 5 appears twice.

I have a much longer cell than this so by hand is not an option.

Upvotes: 1

Views: 51

Answers (1)

Paolo
Paolo

Reputation: 26024

You can use groupcounts:

a = {{1,2,3},{1,2,3,5},{1,2,3},{1,2},{5}};
[gc,gr] = groupcounts(cell2mat([a{:}])')
gc =

     4
     4
     3
     2


gr =

     1
     2
     3
     5

Upvotes: 1

Related Questions