Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: how to find unique rows and calculate the average.
Replies: 9   Last Post: Apr 12, 2013 6:31 PM

 Messages: [ Previous | Next ]
 dpb Posts: 9,850 Registered: 6/7/07
Re: how to find unique rows and calculate the average.
Posted: Apr 10, 2013 8:37 PM

On 4/10/2013 4:10 PM, Alan Hitch wrote:
> dpb <none@non.net> wrote in message <kk2k8a\$h1m\$1@speranza.aioe.org>...
...

>> Well, not seeing what you tried can't try to fix it, but one
>> straightforward approach...
>>

>> >> type 'alan.dat'
>>
>> 3BAB Brackish 27-Oct-04 PAPU 2 Fall 2004 3BAB38287PAPU 35 25 9 0 1 0 0
>> 0 0 0 0
>> 3BAB Brackish 27-Oct-04 POLA 2 Fall 2004 3BAB38287POLA 79 46 19 9 5 0
>> 0 0 0 0 0

...

>> >> fid=fopen('alan.dat','rt');
>> >> C=textscan(fid,[repmat('%*s',1,7) '%s'

>> repmat('%d',1,11)],'collectoutput',1);
>> >> fid=fclose(fid);
>> >> u=unique(C{1});
>> >> for i=1:length(u),m=mean(C{2}(strcmp(C{1},u(i)),1));disp([u(i) m]),end

>> '3BAB38287CYVA' [11]
...
>> '3BAB38287POLA' [79]
>> >>
>>
>> Note I didn't worry about the header and for simplicity of demo just
>> threw away all the data columns up to the uniqeID one. Then used
>> 'collectoutput' to put rest in a single cell.
>>
>> Then we found the unique IDs that are in the file (of which there is
>> only one of each in the sample and ran a loop over it to find the mean
>> by using a logical vector returned by strcmp as the row index into the
>> data. I chose the first column arbitrarily.
>>
>> Salt to suit for your case, of course...

...

> Thanks for responding. I neglected to tell you that the data is a cell
> array with different data types and textscan only works with double or
> string. I read the data in using xlsread and was using the raw data. I
> am relatively new to MatLab. How do I convert the cell array into a
> double or string? I tried using cell2mat but the contents of my
> cellarray are different data types.

textscan() can read whatever you give it the formatting for; see above
that reads your file as you said it was/is. All you need is to use the
appropriate 'headerlines',N option to skip them.

Given the above you have

3BAB Brackish 27-Oct-04 PAPU 2 Fall 2004 3BAB38287PAPU

a 2 strings, a date (read as a string also is simplest to deal with), a
string, an integer, a string, integer, string followed by 8 numerics.

Expand the format string I gave as needed to return any/all fields you
wish/need; the 'collectoutput' flag will put them into two cells one
containing the strings, the other the values.

I don't see where the problem is; why can't you use what gave [almost]
directly?

--

Date Subject Author
4/9/13 Guest
4/9/13 dpb
4/10/13 Alan Hitch
4/10/13 dpb
4/10/13 dpb
4/11/13 Alan Hitch
4/11/13 dpb
4/12/13 dpb
4/10/13 Peter Perkins
4/11/13 Bruno Luong