Thread Subject: "mode" function for mixed types

Subject: "mode" function for mixed types

From: David Doria

Date: 30 Nov, 2007 22:19:09

Message: 1 of 11

I have a cell array like

[1] [1] [2] [a] [1] [a] [2] [a]

and i want to determine which is the most frequently
occurring "character" (similar to the "mode" of a strictly
integer array).

Is there a function that will do this? Or do i have to go
through the array and keep track of which things I have
already counted and all that business?

Thanks,

David

Subject: "mode" function for mixed types

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 30 Nov, 2007 22:45:03

Message: 2 of 11

In article <fiq28t$6qq$1@fred.mathworks.com>,
David Doria <daviddoria@gmail.com> wrote:
>I have a cell array like

>[1] [1] [2] [a] [1] [a] [2] [a]

>and i want to determine which is the most frequently
>occurring "character" (similar to the "mode" of a strictly
>integer array).

>Is there a function that will do this?

hist() or histc(). The easiest way to convert the characters
to numbers is to add 0 to the character: e.g.,

 'hello' + 0

will be a numeric array of length 5 containing the numeric codes
for each character.
--
  "There are some ideas so wrong that only a very intelligent person
  could believe in them." -- George Orwell

Subject: Re:

From: David Doria

Date: 30 Nov, 2007 23:17:34

Message: 3 of 11

that actually might work in this case since i'll probably
have a one to one mapping from character sets to summed
ascii numbers. however, if i was not sure i would have this
one to one mapping , my original question of whether there
is some sort of function that already does this holds.

thanks,

david

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fiq3pf$4oc$1@canopus.cc.umanitoba.ca>...
> In article <fiq28t$6qq$1@fred.mathworks.com>,
> David Doria <daviddoria@gmail.com> wrote:
> >I have a cell array like
>
> >[1] [1] [2] [a] [1] [a] [2] [a]
>
> >and i want to determine which is the most frequently
> >occurring "character" (similar to the "mode" of a strictly
> >integer array).
>
> >Is there a function that will do this?
>
> hist() or histc(). The easiest way to convert the characters
> to numbers is to add 0 to the character: e.g.,
>
> 'hello' + 0
>
> will be a numeric array of length 5 containing the numeric
codes
> for each character.
> --
> "There are some ideas so wrong that only a very
intelligent person
> could believe in them." --
George Orwell

Subject: Re:

From: Bruno Luong

Date: 30 Nov, 2007 23:33:02

Message: 4 of 11

"David Doria" <daviddoria@gmail.com> wrote in message
<fiq5me$64a$1@fred.mathworks.com>...
> that actually might work in this case since i'll probably
> have a one to one mapping from character sets to summed
> ascii numbers. however, if i was not sure i would have this
> one to one mapping , my original question of whether there
> is some sort of function that already does this holds.
>

Can you just call two modes, one for char, once for integer?

c={1 2 1 'a' 3 'b' 'a' 2 'a' 10};

[mostc fc]=mode(double(cell2mat(c(cellfun(@ischar,c)))))
[mosti fi]=mode(double(cell2mat(c(cellfun(@isnumeric,c)))))

if fc>fi % char wins
  mostelem=char(mostc)
else % integer wins
  mostelem=mosti
end

Bruno

Subject: Re:

From: David Doria

Date: 1 Dec, 2007 02:29:41

Message: 5 of 11

ahh thats a good idea
I'll have to look into that code this weeeknd - i never
really followed the @ in matlab, but I haven't done my
research on it!

thanks!

"Bruno Luong" <brunoluong@yahoo.com> wrote in message
<fiq6je$k8t$1@fred.mathworks.com>...
> "David Doria" <daviddoria@gmail.com> wrote in message
> <fiq5me$64a$1@fred.mathworks.com>...
> > that actually might work in this case since i'll probably
> > have a one to one mapping from character sets to summed
> > ascii numbers. however, if i was not sure i would have this
> > one to one mapping , my original question of whether there
> > is some sort of function that already does this holds.
> >
>
> Can you just call two modes, one for char, once for integer?
>
> c={1 2 1 'a' 3 'b' 'a' 2 'a' 10};
>
> [mostc fc]=mode(double(cell2mat(c(cellfun(@ischar,c)))))
> [mosti fi]=mode(double(cell2mat(c(cellfun(@isnumeric,c)))))
>
> if fc>fi % char wins
> mostelem=char(mostc)
> else % integer wins
> mostelem=mosti
> end
>
> Bruno
>

Subject: Re:

From: David Doria

Date: 1 Dec, 2007 21:03:43

Message: 6 of 11

thanks alot. the function handle @ is very easy to
understand when you read the help! haha

dave

"David Doria" <daviddoria@gmail.com> wrote in message
<fiqgul$r2o$1@fred.mathworks.com>...
> ahh thats a good idea
> I'll have to look into that code this weeeknd - i never
> really followed the @ in matlab, but I haven't done my
> research on it!
>
> thanks!
>
> "Bruno Luong" <brunoluong@yahoo.com> wrote in message
> <fiq6je$k8t$1@fred.mathworks.com>...
> > "David Doria" <daviddoria@gmail.com> wrote in message
> > <fiq5me$64a$1@fred.mathworks.com>...
> > > that actually might work in this case since i'll probably
> > > have a one to one mapping from character sets to summed
> > > ascii numbers. however, if i was not sure i would
have this
> > > one to one mapping , my original question of whether there
> > > is some sort of function that already does this holds.
> > >
> >
> > Can you just call two modes, one for char, once for integer?
> >
> > c={1 2 1 'a' 3 'b' 'a' 2 'a' 10};
> >
> > [mostc fc]=mode(double(cell2mat(c(cellfun(@ischar,c)))))
> > [mosti fi]=mode(double(cell2mat(c(cellfun(@isnumeric,c)))))
> >
> > if fc>fi % char wins
> > mostelem=char(mostc)
> > else % integer wins
> > mostelem=mosti
> > end
> >
> > Bruno
> >
>

Subject: Re:

From: David Doria

Date: 1 Dec, 2007 21:04:07

Message: 7 of 11

thanks alot. the function handle @ is very easy to
understand when you read the help! haha

dave

"David Doria" <daviddoria@gmail.com> wrote in message
<fiqgul$r2o$1@fred.mathworks.com>...
> ahh thats a good idea
> I'll have to look into that code this weeeknd - i never
> really followed the @ in matlab, but I haven't done my
> research on it!
>
> thanks!
>
> "Bruno Luong" <brunoluong@yahoo.com> wrote in message
> <fiq6je$k8t$1@fred.mathworks.com>...
> > "David Doria" <daviddoria@gmail.com> wrote in message
> > <fiq5me$64a$1@fred.mathworks.com>...
> > > that actually might work in this case since i'll probably
> > > have a one to one mapping from character sets to summed
> > > ascii numbers. however, if i was not sure i would
have this
> > > one to one mapping , my original question of whether there
> > > is some sort of function that already does this holds.
> > >
> >
> > Can you just call two modes, one for char, once for integer?
> >
> > c={1 2 1 'a' 3 'b' 'a' 2 'a' 10};
> >
> > [mostc fc]=mode(double(cell2mat(c(cellfun(@ischar,c)))))
> > [mosti fi]=mode(double(cell2mat(c(cellfun(@isnumeric,c)))))
> >
> > if fc>fi % char wins
> > mostelem=char(mostc)
> > else % integer wins
> > mostelem=mosti
> > end
> >
> > Bruno
> >
>

Subject: Re:

From: David Doria

Date: 1 Dec, 2007 21:04:08

Message: 8 of 11

thanks alot. the function handle @ is very easy to
understand when you read the help! haha

dave

"David Doria" <daviddoria@gmail.com> wrote in message
<fiqgul$r2o$1@fred.mathworks.com>...
> ahh thats a good idea
> I'll have to look into that code this weeeknd - i never
> really followed the @ in matlab, but I haven't done my
> research on it!
>
> thanks!
>
> "Bruno Luong" <brunoluong@yahoo.com> wrote in message
> <fiq6je$k8t$1@fred.mathworks.com>...
> > "David Doria" <daviddoria@gmail.com> wrote in message
> > <fiq5me$64a$1@fred.mathworks.com>...
> > > that actually might work in this case since i'll probably
> > > have a one to one mapping from character sets to summed
> > > ascii numbers. however, if i was not sure i would
have this
> > > one to one mapping , my original question of whether there
> > > is some sort of function that already does this holds.
> > >
> >
> > Can you just call two modes, one for char, once for integer?
> >
> > c={1 2 1 'a' 3 'b' 'a' 2 'a' 10};
> >
> > [mostc fc]=mode(double(cell2mat(c(cellfun(@ischar,c)))))
> > [mosti fi]=mode(double(cell2mat(c(cellfun(@isnumeric,c)))))
> >
> > if fc>fi % char wins
> > mostelem=char(mostc)
> > else % integer wins
> > mostelem=mosti
> > end
> >
> > Bruno
> >
>

Subject: Re:

From: David Doria

Date: 1 Dec, 2007 21:04:09

Message: 9 of 11

thanks alot. the function handle @ is very easy to
understand when you read the help! haha

dave

"David Doria" <daviddoria@gmail.com> wrote in message
<fiqgul$r2o$1@fred.mathworks.com>...
> ahh thats a good idea
> I'll have to look into that code this weeeknd - i never
> really followed the @ in matlab, but I haven't done my
> research on it!
>
> thanks!
>
> "Bruno Luong" <brunoluong@yahoo.com> wrote in message
> <fiq6je$k8t$1@fred.mathworks.com>...
> > "David Doria" <daviddoria@gmail.com> wrote in message
> > <fiq5me$64a$1@fred.mathworks.com>...
> > > that actually might work in this case since i'll probably
> > > have a one to one mapping from character sets to summed
> > > ascii numbers. however, if i was not sure i would
have this
> > > one to one mapping , my original question of whether there
> > > is some sort of function that already does this holds.
> > >
> >
> > Can you just call two modes, one for char, once for integer?
> >
> > c={1 2 1 'a' 3 'b' 'a' 2 'a' 10};
> >
> > [mostc fc]=mode(double(cell2mat(c(cellfun(@ischar,c)))))
> > [mosti fi]=mode(double(cell2mat(c(cellfun(@isnumeric,c)))))
> >
> > if fc>fi % char wins
> > mostelem=char(mostc)
> > else % integer wins
> > mostelem=mosti
> > end
> >
> > Bruno
> >
>

Subject: Re:

From: David Doria

Date: 1 Dec, 2007 21:29:50

Message: 10 of 11

any idea why this got posted so many times?

"David Doria" <daviddoria@gmail.com> wrote in message
<fisi89$1uk$1@fred.mathworks.com>...
> thanks alot. the function handle @ is very easy to
> understand when you read the help! haha
>
> dave
>
> "David Doria" <daviddoria@gmail.com> wrote in message
> <fiqgul$r2o$1@fred.mathworks.com>...
> > ahh thats a good idea
> > I'll have to look into that code this weeeknd - i never
> > really followed the @ in matlab, but I haven't done my
> > research on it!
> >
> > thanks!
> >
> > "Bruno Luong" <brunoluong@yahoo.com> wrote in message
> > <fiq6je$k8t$1@fred.mathworks.com>...
> > > "David Doria" <daviddoria@gmail.com> wrote in message
> > > <fiq5me$64a$1@fred.mathworks.com>...
> > > > that actually might work in this case since i'll
probably
> > > > have a one to one mapping from character sets to summed
> > > > ascii numbers. however, if i was not sure i would
> have this
> > > > one to one mapping , my original question of whether
there
> > > > is some sort of function that already does this holds.
> > > >
> > >
> > > Can you just call two modes, one for char, once for
integer?
> > >
> > > c={1 2 1 'a' 3 'b' 'a' 2 'a' 10};
> > >
> > > [mostc fc]=mode(double(cell2mat(c(cellfun(@ischar,c)))))
> > > [mosti
fi]=mode(double(cell2mat(c(cellfun(@isnumeric,c)))))
> > >
> > > if fc>fi % char wins
> > > mostelem=char(mostc)
> > > else % integer wins
> > > mostelem=mosti
> > > end
> > >
> > > Bruno
> > >
> >
>

Subject: Re:

From: Bruno Luong

Date: 1 Dec, 2007 21:31:27

Message: 11 of 11

"David Doria" <daviddoria@gmail.com> wrote in message
<fisjoe$idh$1@fred.mathworks.com>...
> any idea why this got posted so many times?
>
echo of "hahha"?

Bruno

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com