The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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 ]

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));

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?



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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.