Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Creating a new vector based on unique entries
Date: Thu, 14 May 2009 19:33:01 +0000 (UTC)
Organization: Universit&#228;tsSpital Z&#252;rich
Lines: 40
Message-ID: <guhrld$m89$1@fred.mathworks.com>
References: <guh7ap$s7n$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1242329581 22793 172.30.248.35 (14 May 2009 19:33:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 14 May 2009 19:33:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 11
Xref: news.mathworks.com comp.soft-sys.matlab:540023


"Anna Chen" <icedredtea@yahoo.com> wrote in message <guh7ap$s7n$1@fred.mathworks.com>...
> Hello there,
> I have a question on something that I just can't seem to get to work correctly.  I have a dataset imported from excel that looks like this:
> 
> cat          2
> cat          3
> cat          0
> dog         4
> dog         5
> mouse     6
> 
> Where the first column is cell and the second is double.  How would I create a vector that has three elements, the first that averages all the "cats," the second that averages all the "dogs" and the third that averages all the "mice"?  I've been trying combinations of for and while loops, but the different data classes are messing me up!
> 
> Thanks!

one of the many solutions

% the data
% - note: your data combined in one cell for sake of brevity...
     d={
          'cat'          1
          'cat'          2
          'dog'          4
          'cat'          3
          'dog'          8
          'mouse'          -10
     };
% the engine
     nÊt(1,d{:,2});     % <- your 2nd data set...
     [du,ix,ix]=unique(d(:,1));
     r¬cumarray(ix,n,[],@mean);
% the result
     disp([du,num2cell(r)]);
%{
    'cat'      [  2]
    'dog'      [  6]
    'mouse'    [-10]
%}

us