For Programmers: Free Programming Magazines  


Home > Archive > Matlab > August 2005 > Most frequent element in array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Most frequent element in array
Nuno Benavente

2005-05-01, 9:17 pm

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
CDMA_RF_ENG

2005-05-01, 9:17 pm

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



per isakson

2005-05-01, 9:17 pm

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
Anders Björk

2005-05-01, 9:17 pm

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



Roger Stafford

2005-05-01, 9:17 pm

In article <dOSce.52547$Of5.33494@nntpserver.swip.net>, "Anders Björk"
<huanbj@hotmail.com> wrote:
[color=darkred]
> 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
Nuno Benavente

2005-05-01, 9:17 pm

Thank you for your replies!

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

2005-05-01, 9:17 pm

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
Rajeev

2005-05-02, 4:01 am

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-

Brett Shoelson

2005-05-02, 4:01 am

<SNIP mode discussion>
Note for the record that Michael Robbins posted to the FEX a nice
little wrapper (mode.m) for this.
Brett
Rajeev

2005-05-02, 4:03 pm

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-

frank

2005-08-30, 7:58 am

I am now doing a similar project where I have got on hand a [1000 plus x 7] array where each row represent a unique incidence of pattern and they number is fall into the range of 1 - 49 and it is unique amongst each of the row. e.g. [1 2 3 4 5 6 7; 2 3 4
5 6 7 8; 3 4 5 6 7 8 9]

Now I need to work out the frequent patterns and don't have any idea on how to reach it. The result of the above example matrix should be [3 4 5 6 7].

Any help is much appreciated.
John D'Errico

2005-08-30, 7:58 am

In article
<2471772.1125395873833.JavaMail.jakarta@nitrogen.mathforum.org>,
frank <kwcheong@gmail.com> wrote:

> I am now doing a similar project where I have got on hand a [1000 plus x 7]
> array where each row represent a unique incidence of pattern and they number
> is fall into the range of 1 - 49 and it is unique amongst each of the row.
> e.g. [1 2 3 4 5 6 7; 2 3 4 5 6 7 8; 3 4 5 6 7 8 9]
>
> Now I need to work out the frequent patterns and don't have any idea on how
> to reach it. The result of the above example matrix should be [3 4 5 6 7].
>
> Any help is much appreciated.



Suppose you are looking for sequences of length 5.

Each row has 3 such possible sequences. So convert
your problem into an array of size 3000 x 5. Now
search for the most common rows. For example:

A = ceil(rand(1000,7)*49);

n = 5; % desired sub-sequence length

B = A(:,1:n);
for i = 1:(7-n)
B = [B;A(:,i+(1:n))];
end

% count the number of times each sequence shows
% up. I'll use consolidator from matlab central.
[subseq,count] = consolidator(B,[],'count');

Subseq will be all the unique subsequences that were
found, and count is the number of times that sequence
was found. If count is greater than 1, then a given
sequence was replicated.

In my poor random sample, there were no replicated
sequences of length 5. But when I chose n = 3, I
found this sequence replicated 3 times:

subseq(64,:)
ans =
1 25 35

max(count)
ans =
3

When I repeated the search for sequences of length 2,
I found the sub-sequence [34 49] showed up 10 times.


You can find consolidator at this url:

http://www.mathworks.com/matlabcent...le.do?objectId=
8354&objectType=file

I hope to put a new version up there today.

HTH,
John D'Errico


--
The best material model of a cat is another, or
preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945
frank

2005-08-31, 7:57 am

it worked. but the result is a scaler, can I obtain an matrix if say there are more than one item >= max.
John D'Errico

2005-08-31, 7:01 pm

In article
<19551381.1125492379931.JavaMail.jakarta@nitrogen.mathforum.org>,
frank <kwcheong@gmail.com> wrote:

> it worked. but the result is a scaler, can I obtain an matrix if say there
> are more than one item >= max.


I'm not certain what you mean.

In the example I gave, just use

subseq(count==max(count),:)

This will give a list of the sequences that were found
most often. If there were more than 1, it will list
them all.

John


--
The best material model of a cat is another, or
preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945
frank

2005-08-31, 7:01 pm

it worked. Thx.

Is there any good resource that you can point me to so as to get familiar with MatLab ?
John D'Errico

2005-08-31, 7:01 pm

In article
<9150953.1125498606746.JavaMail.jakarta@nitrogen.mathforum.org>,
frank <kwcheong@gmail.com> wrote:

> it worked. Thx.
>
> Is there any good resource that you can point me to so as to get familiar
> with MatLab ?


This is a harder question for me, since I've never really
read the official matlab documentation, not in the last
15 years. I may have in the '80s sometime, but its
probably changed. I gather its quite good though. (A
friend learned to use the image processing toolbox from
the online tutorials. He was happy with the result.)

"Mastering Matlab 7" (by Hanselman & Littlefield) was
often my recommendation to new users at work.

This newsgroup is an excellent resource. I'm always
learning new tidbits and tricks here.

There are many texts on the use of matlab in one
context or another. Cleve Moler has written one. I'd
look on their website for any specific areas of
interest.

Matlab central has some compilations of tips and tricks.
Look in the appropriate category in the file exchange.

http://www.mathworks.com/matlabcent...loadCategory.do

There is a Matlab FAQ.

http://www.mit.edu/~pwb/cssm/

HTH,
John


--
The best material model of a cat is another, or
preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com