Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Is there a better way to count number of occurances of string when grouping?
Replies: 6   Last Post: Mar 20, 2012 6:11 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Tyler

Posts: 79
Registered: 7/20/08
Is there a better way to count number of occurances of string when grouping?
Posted: Mar 20, 2012 2:15 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hi everyone:

I have a dataset svdatm with columns:

% dnum (datenum)
% symid (symbol id - a cell array)

I would like to find the count or number of elements (i.e., symid)
that occur each day.

% Use a loop - this becomes slow for large array
mydates = unique(svdatm.dnum);
NDAYS = length(mydates);
symcount = nan(NDAYS,1);
for i = 1:NDAYS
idxDay = svdatm.dnum==mydates(i);
tmpD = svdatm(idxDay,:);
symcount(i) = length(unique(tmpD.symid));
end

I've tried to speed up the process using something like the following,

symcount = unstack(svdatm,'symid','dnum','AggregationFun',@numel);

or similarly grpstats, but these all bomb because it is a cell array I
am applying the function too. Is there a better way to do this?

Cheers,

t.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.