Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Most frequent element in array
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

Report this thread to moderator Post Follow-up to this message
Old Post
Nuno Benavente
05-02-05 02:17 AM


Re: Most frequent element in array
help 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



Report this thread to moderator Post Follow-up to this message
Old Post
CDMA_RF_ENG
05-02-05 02:17 AM


Re: Most frequent element in array
Nuno 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

Report this thread to moderator Post Follow-up to this message
Old Post
per isakson
05-02-05 02:17 AM


Re: Most frequent element in array
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
>
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
Anders Björk
05-02-05 02:17 AM


Re: Most frequent element in array
In 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

Report this thread to moderator Post Follow-up to this message
Old Post
Roger Stafford
05-02-05 02:17 AM


Re: Most frequent element in array
Thank you for your replies!

Roger, your solution worked perfectly! Thank you so much :)

Report this thread to moderator Post Follow-up to this message
Old Post
Nuno Benavente
05-02-05 02:17 AM


Re: Most frequent element in array
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

Report this thread to moderator Post Follow-up to this message
Old Post
us
05-02-05 02:17 AM


Re: Most frequent element in array
us 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-


Report this thread to moderator Post Follow-up to this message
Old Post
Rajeev
05-02-05 09:01 AM


Re: Most frequent element in array
<SNIP mode discussion>
Note for the record that Michael Robbins posted to the FEX a nice
little wrapper (mode.m) for this.
Brett

Report this thread to moderator Post Follow-up to this message
Old Post
Brett Shoelson
05-02-05 09:01 AM


Re: Most frequent element in array
Brett 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-


Report this thread to moderator Post Follow-up to this message
Old Post
Rajeev
05-02-05 09:03 PM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

Matlab archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:50 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.