http://www.mathworks.com/matlabcentral/newsreader/view_thread/305296
MATLAB Central Newsreader  Counting Genotypes from Alleles
Feed for thread: Counting Genotypes from Alleles
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 29 Mar 2011 17:19:04 +0000
Counting Genotypes from Alleles
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305296#828109
Amanda
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.<br>
<br>
For example for the following vector, how many times does A occur in column 1 along with A in column 2. <br>
vec=[A A<br>
A A<br>
A A<br>
A a<br>
A A<br>
a a]<br>
Where the answer would be 4. <br>
Thank you!

Tue, 29 Mar 2011 17:31:04 +0000
Re: Counting Genotypes from Alleles
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305296#828115
Sean de
"Amanda " <ahulse@tamu.edu> wrote in message <imt4a8$nh$1@fred.mathworks.com>...<br>
> 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.<br>
> <br>
> For example for the following vector, how many times does A occur in column 1 along with A in column 2. <br>
> vec=[A A<br>
> A A<br>
> A A<br>
> A a<br>
> A A<br>
> a a]<br>
> Where the answer would be 4. <br>
> Thank you!<br>
<br>
Assuming you can have numeric values instead of letters:<br>
example:<br>
<br>
A = [1 1; 2 3; 1 3; 1 1; 1 1; 2 3; 3 4; 2 4; 1 1]<br>
N = sum(all(bsxfun(@eq,A,[1 1]),2)) %how many [1 1]s

Tue, 29 Mar 2011 17:52:22 +0000
Re: Counting Genotypes from Alleles
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305296#828126
Florin Neacsu
"Amanda " <ahulse@tamu.edu> wrote in message <imt4a8$nh$1@fred.mathworks.com>...<br>
> 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.<br>
> <br>
> For example for the following vector, how many times does A occur in column 1 along with A in column 2. <br>
> vec=[A A<br>
> A A<br>
> A A<br>
> A a<br>
> A A<br>
> a a]<br>
> Where the answer would be 4. <br>
> Thank you!<br>
<br>
Hi,<br>
<br>
try ismember :<br>
>sum(ismember(vec,[A A],'rows'))<br>
<br>
Regards,<br>
Florin

Tue, 29 Mar 2011 19:27:03 +0000
Re: Counting Genotypes from Alleles
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305296#828138
Roger Stafford
"Amanda " <ahulse@tamu.edu> wrote in message <imt4a8$nh$1@fred.mathworks.com>...<br>
> 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.<br>
> <br>
> For example for the following vector, how many times does A occur in column 1 along with A in column 2. <br>
> vec=[A A<br>
> A A<br>
> A A<br>
> A a<br>
> A A<br>
> a a]<br>
> Where the answer would be 4. <br>
> Thank you!<br>
          <br>
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.<br>
<br>
[u,~,n] = unique(vec,'rows');<br>
list = [u,diff(find([true;diff(sort(n))>0;true]))];<br>
<br>
Then 'list' will be a threecolumn array with unique pairs in the first two columns and their corresponding occurrence counts in the third column.<br>
<br>
Roger Stafford

Tue, 29 Mar 2011 19:29:21 +0000
Re: Counting Genotypes from Alleles
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305296#828139
Amanda
"Florin Neacsu" <fneacsu2@gmail.com> wrote in message <imt68m$4s1$1@fred.mathworks.com>...<br>
> "Amanda " <ahulse@tamu.edu> wrote in message <imt4a8$nh$1@fred.mathworks.com>...<br>
> > 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.<br>
> > <br>
> > For example for the following vector, how many times does A occur in column 1 along with A in column 2. <br>
> > vec=[A A<br>
> > A A<br>
> > A A<br>
> > A a<br>
> > A A<br>
> > a a]<br>
> > Where the answer would be 4. <br>
> > Thank you!<br>
> <br>
> Hi,<br>
> <br>
> try ismember :<br>
> >sum(ismember(vec,[A A],'rows'))<br>
> <br>
> Regards,<br>
> Florin<br>
<br>
<br>
Thank you!

Tue, 29 Mar 2011 21:14:20 +0000
Re: Counting Genotypes from Alleles
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305296#828157
Florin Neacsu
"Amanda " <ahulse@tamu.edu> wrote in message <imtbuh$f61$1@fred.mathworks.com>...<br>
> "Florin Neacsu" <fneacsu2@gmail.com> wrote in message <imt68m$4s1$1@fred.mathworks.com>...<br>
> > "Amanda " <ahulse@tamu.edu> wrote in message <imt4a8$nh$1@fred.mathworks.com>...<br>
> > > 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.<br>
> > > <br>
> > > For example for the following vector, how many times does A occur in column 1 along with A in column 2. <br>
> > > vec=[A A<br>
> > > A A<br>
> > > A A<br>
> > > A a<br>
> > > A A<br>
> > > a a]<br>
> > > Where the answer would be 4. <br>
> > > Thank you!<br>
> > <br>
> > Hi,<br>
> > <br>
> > try ismember :<br>
> > >sum(ismember(vec,[A A],'rows'))<br>
> > <br>
> > Regards,<br>
> > Florin<br>
> <br>
> <br>
> Thank you!<br>
<br>
Hi,<br>
<br>
You're welcome.<br>
<br>
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<br>
<br>
>list = [u,num2str(diff(find([true;diff(sort(n))>0;true])))];<br>
<br>
Regards,<br>
Florin