Path: news.mathworks.com!newsfeed-00.mathworks.com!kanaga.switch.ch!switch.ch!newsfeed00.sul.t-online.de!newsmm00.sul.t-online.de!t-online.de!news.t-online.com!not-for-mail
From: Lothar Schmidt <vapooroop@gmx.net>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Creating a new vector based on unique entries
Date: Thu, 14 May 2009 18:08:18 +0200
Organization: T-Online
Lines: 52
Message-ID: <guhfp9$495$03$1@news.t-online.com>
References: <guh7ap$s7n$1@fred.mathworks.com> <guh86e$5iq$01$1@news.t-online.com> <guh9i2$r8f$1@fred.mathworks.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.t-online.com 1242317417 03 n4389 AcKQQK5lCC8n46a 090514 16:10:17
X-Complaints-To: usenet-abuse@t-online.de
X-ID: TDJBCUZ1YeGykaxlVKAadbFsN1ih2+pcCj0xCLJIMADGDB1rvk97r4
User-Agent: Thunderbird 2.0.0.21 (Windows/20090302)
In-Reply-To: <guh9i2$r8f$1@fred.mathworks.com>
Xref: news.mathworks.com comp.soft-sys.matlab:539977


Anna Chen schrieb:
> Thanks for everyone's help!  I guess i was having trouble with the string matching thing.
> Lothar, I have a question for you.  when you do find(strcmp(tmp, animal.type)), won't that not work because tmp will be a cell with 3 elements and animal.type has 5?
> Just wanted to understand the logic and improve my skills!
> 
> 
> Lothar Schmidt <vapooroop@gmx.net> wrote in message <guh86e$5iq$01$1@news.t-online.com>...
>> Anna Chen schrieb:
>>> 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!
>> like this?
>>
>> k=0;
>> for tmp=unique(animal.type),
>> 	k=k+1;
>> 	list.name{k}=tmp;
>> 	index=find(strcmp(tmp,animal.type));
>> 	list.mean(k)=mean(animal.num(index));
>> end
>> list

supposing that

animal.type{1}='cat'
animal.type{2}='dog'
...
animal.num(1)=4
animal.num(2)=7
...

tmp will be a cell with one og the anymal types.

strcmp(tmp,animal.type)

will compare any animal type to the current tmp (type of animal) and 
will give you 1 if type=tmp and 0 if type~=tmp
find(this_logical) will give you the index of identical animaltypes

mean(animal.num(index)) gives the mean of the appropriate numbers

is this the answer to your question?