Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Counting Genotypes from Alleles

Subject: Counting Genotypes from Alleles

From: Amanda

Date: 29 Mar, 2011 17:19:04

Message: 1 of 6

I am trying to count the number of times a combination of alleles occur in a single matrix. Does anyone know how to write a correct function for this? I have looked into the 'unique' command however it does not seem to give me the correct number.

For example for the following vector, how many times does A occur in column 1 along with A in column 2.
vec=[A A
A A
A A
A a
A A
a a]
Where the answer would be 4.
Thank you!

Subject: Counting Genotypes from Alleles

From: Sean de

Date: 29 Mar, 2011 17:31:04

Message: 2 of 6

"Amanda " <ahulse@tamu.edu> wrote in message <imt4a8$nh$1@fred.mathworks.com>...
> I am trying to count the number of times a combination of alleles occur in a single matrix. Does anyone know how to write a correct function for this? I have looked into the 'unique' command however it does not seem to give me the correct number.
>
> For example for the following vector, how many times does A occur in column 1 along with A in column 2.
> vec=[A A
> A A
> A A
> A a
> A A
> a a]
> Where the answer would be 4.
> Thank you!

Assuming you can have numeric values instead of letters:
example:

A = [1 1; 2 3; 1 3; 1 1; 1 1; 2 3; 3 4; 2 4; 1 1]
N = sum(all(bsxfun(@eq,A,[1 1]),2)) %how many [1 1]s

Subject: Counting Genotypes from Alleles

From: Florin Neacsu

Date: 29 Mar, 2011 17:52:22

Message: 3 of 6

"Amanda " <ahulse@tamu.edu> wrote in message <imt4a8$nh$1@fred.mathworks.com>...
> I am trying to count the number of times a combination of alleles occur in a single matrix. Does anyone know how to write a correct function for this? I have looked into the 'unique' command however it does not seem to give me the correct number.
>
> For example for the following vector, how many times does A occur in column 1 along with A in column 2.
> vec=[A A
> A A
> A A
> A a
> A A
> a a]
> Where the answer would be 4.
> Thank you!

Hi,

try ismember :
>sum(ismember(vec,[A A],'rows'))

Regards,
Florin

Subject: Counting Genotypes from Alleles

From: Roger Stafford

Date: 29 Mar, 2011 19:27:03

Message: 4 of 6

"Amanda " <ahulse@tamu.edu> wrote in message <imt4a8$nh$1@fred.mathworks.com>...
> I am trying to count the number of times a combination of alleles occur in a single matrix. Does anyone know how to write a correct function for this? I have looked into the 'unique' command however it does not seem to give me the correct number.
>
> For example for the following vector, how many times does A occur in column 1 along with A in column 2.
> vec=[A A
> A A
> A A
> A a
> A A
> a a]
> Where the answer would be 4.
> Thank you!
- - - - - - - - - - -
  If you want a list of all unique pairs occurring in 'vec' along with their corresponding counts, you can use the 'unique' function with the 'rows' option specified.

 [u,~,n] = unique(vec,'rows');
 list = [u,diff(find([true;diff(sort(n))>0;true]))];

Then 'list' will be a three-column array with unique pairs in the first two columns and their corresponding occurrence counts in the third column.

Roger Stafford

Subject: Counting Genotypes from Alleles

From: Amanda

Date: 29 Mar, 2011 19:29:21

Message: 5 of 6

"Florin Neacsu" <fneacsu2@gmail.com> wrote in message <imt68m$4s1$1@fred.mathworks.com>...
> "Amanda " <ahulse@tamu.edu> wrote in message <imt4a8$nh$1@fred.mathworks.com>...
> > I am trying to count the number of times a combination of alleles occur in a single matrix. Does anyone know how to write a correct function for this? I have looked into the 'unique' command however it does not seem to give me the correct number.
> >
> > For example for the following vector, how many times does A occur in column 1 along with A in column 2.
> > vec=[A A
> > A A
> > A A
> > A a
> > A A
> > a a]
> > Where the answer would be 4.
> > Thank you!
>
> Hi,
>
> try ismember :
> >sum(ismember(vec,[A A],'rows'))
>
> Regards,
> Florin


Thank you!

Subject: Counting Genotypes from Alleles

From: Florin Neacsu

Date: 29 Mar, 2011 21:14:20

Message: 6 of 6

"Amanda " <ahulse@tamu.edu> wrote in message <imtbuh$f61$1@fred.mathworks.com>...
> "Florin Neacsu" <fneacsu2@gmail.com> wrote in message <imt68m$4s1$1@fred.mathworks.com>...
> > "Amanda " <ahulse@tamu.edu> wrote in message <imt4a8$nh$1@fred.mathworks.com>...
> > > I am trying to count the number of times a combination of alleles occur in a single matrix. Does anyone know how to write a correct function for this? I have looked into the 'unique' command however it does not seem to give me the correct number.
> > >
> > > For example for the following vector, how many times does A occur in column 1 along with A in column 2.
> > > vec=[A A
> > > A A
> > > A A
> > > A a
> > > A A
> > > a a]
> > > Where the answer would be 4.
> > > Thank you!
> >
> > Hi,
> >
> > try ismember :
> > >sum(ismember(vec,[A A],'rows'))
> >
> > Regards,
> > Florin
>
>
> Thank you!

Hi,

You're welcome.

Also, you should take into consideration Roger's suggestion (it provides more information). In case you are using chars and not integers for a and A, then use

>list = [u,num2str(diff(find([true;diff(sort(n))>0;true])))];

Regards,
Florin

Tags for this Thread

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.

Contact us