Code Comments
Programming Forum and web based access to our favorite programming groups.Hello Is there any matlab function for calculating directly the most frequent element/value in an array? (or preferably, in a sparse matrix). I'm stuck here and fed up with histograms. Thanks in advance
Post Follow-up to this messagehelp hist "Nuno Benavente" <nuno.benavente@ist.utl.pt> wrote in message news:ef04910.-1@webx.raydaftYaTP... > Hello > > Is there any matlab function for calculating directly the most > frequent element/value in an array? (or preferably, in a sparse > matrix). I'm stuck here and fed up with histograms. > > Thanks in advance
Post Follow-up to this messageNuno Benavente wrote: > > > Hello > > Is there any matlab function for calculating directly the most > frequent element/value in an array? (or preferably, in a sparse > matrix). I'm stuck here and fed up with histograms. > > Thanks in advance How come, you ar fed up with histograms? What kind of elements are there in your array. Not floating point numbers, I hope. / per
Post Follow-up to this messagesort and unique in combination will do the job! BR Anders "Nuno Benavente" <nuno.benavente@ist.utl.pt> skrev i meddelandet news:ef04910.-1@webx.raydaftYaTP... > Hello > > Is there any matlab function for calculating directly the most > frequent element/value in an array? (or preferably, in a sparse > matrix). I'm stuck here and fed up with histograms. > > Thanks in advance
Post Follow-up to this messageIn article <dOSce.52547$Of5.33494@nntpserver.swip.net>, "Anders Björk" <huanbj@hotmail.com> wrote: > sort and unique in combination will do the job! > BR > Anders > "Nuno Benavente" <nuno.benavente@ist.utl.pt> skrev i meddelandet > news:ef04910.-1@webx.raydaftYaTP... ------ Hello Nuno, I think 'histc' is the way to go but you have to set up its "edges" parameter properly. Using Anders' idea for 'unique', try this: y = unique(x); n = histc(x,y); % Use the unique values as edges [f,i] = max(n); mf = y(i); % mf is most frequent element of x & it occurs f times Note Per's warning! This will only group element values together that are exactly equal. If you want some tolerance in your "equality", you have a different problem. (Remove "xyzzy" and ".invalid" to send me email.) Roger Stafford
Post Follow-up to this messageThank you for your replies! Roger, your solution worked perfectly! Thank you so much :)
Post Follow-up to this messageRoger Stafford: <SNIP OP wants most frequent element... y=unique(x); n=histc(x,y); [f,i]=max(n); mf=y(i); another solution for this particular problem (we don't really need the overhead of <unique> ) % the data clear all; % save old stuff! n=pi*[10 10 10 12 4 4 2 2 2 2 2 2].'; n(end-2:end)=n(end-2:end)+5*eps; % the engine ns=sort(n); % only part of <unique> c=histc(ns,ns); r=ns(c==max(c)); % the result format long g; [r r-2*pi] just a thought us
Post Follow-up to this messageus wrote: > Roger Stafford: > <SNIP OP wants most frequent element... > > y=unique(x); > n=histc(x,y); > [f,i]=max(n); > mf=y(i); > > another solution for this particular problem (we don't really need > the overhead of <unique> ) > > % the data > clear all; % save old stuff! > n=pi*[10 10 10 12 4 4 2 2 2 2 2 2].'; > n(end-2:end)=n(end-2:end)+5*eps; > % the engine > ns=sort(n); % only part of <unique> > c=histc(ns,ns); > r=ns(c==max(c)); > % the result > format long g; > [r r-2*pi] > > just a thought > us % how about a solution that doesn't use histogram ? n=pi*[10 10 10 12 4 4 2 2 2 2 2 2].'; % test dataset provided by us n(end-2:end)=n(end-2:end)+5*eps; % try with or without this stmt n1=sort(n); % collect like values together n2=diff(n1)==0; % n2=0 at change of value % want longest string of 1's n3=double(n2); % n2=logical, need double n4=find(n2==0); n3(n4)= 1-diff([0;n4]); % this will cancel out running sum n5=cumsum(n3); % longest string of 1's will give max value result=n1(n5==max(n5)); % following us get all values [result result-2*pi] Regards, -rajeev-
Post Follow-up to this message<SNIP mode discussion> Note for the record that Michael Robbins posted to the FEX a nice little wrapper (mode.m) for this. Brett
Post Follow-up to this messageBrett Shoelson wrote: > <SNIP mode discussion> > Note for the record that Michael Robbins posted to the FEX a nice > little wrapper (mode.m) for this. > Brett Thanks for bringing this to my attention. Also for the record, on the File Exchange I found 4/24/04 David Li's function (hist with integer bins) 6/16/04 Michael Robbins' mode function (unique+hist) 11/17/04 Harold Bien's suggestion in response to Michael's post (unique+diff: no hist) Regards, -rajeev-
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.