Thread Subject: sorting data

Subject: sorting data

From: N N

Date: 14 Apr, 2008 08:37:02

Message: 1 of 4

Could someone guide me through the following data sorting
issue please? FYI please: I’ve attached something that
I’ve tried (based on posts in math works forum) below.
Thanks in advance!

My data is this:

2000 0
2006 1
2004 1
2000 1
2006 0
2006 0
2004 1

%column 1 independent variable, column 2 yes/no, yes=1
(ie, correct) & no=0 (ie, incorrect)

My aim is to get the following:

2000 1 2
2004 2 2
2006 1 3

%first column = tested independent variables, 2nd
column=no of correct responses & 3rd column=total number
of testing at each independent variable.
-----------------------------------------------------
My script so far:

a=[
2000 0
2006 1
2004 1
2000 1
2006 0
2006 0
2004 1
] %data
 
b=sortrows(a) %sorts in ascending row
 
c=unique(b(:,1))%gives me a column of independent
variables (I am stuck here)

Subject: sorting data

From: Kris De Gussem

Date: 14 Apr, 2008 09:00:28

Message: 2 of 4

Hi,

You should use logical indexing (http://tinyurl.com/yfw4n9). By comparing the
first column with one of the unique elements, you get a vector of ones where the
element is equal to that element. This matrix already gives you the number of
times the value is present. And in addition, it tells you which elements of the
second column you need to study (see code below).

Regards
Kris

a=[
2000 0
2006 1
2004 1
2000 1
2006 0
2004 1
] %data
b=unique(a(:,1));
le=length(b);
c=zeros(le,3);
for i=1:le
     c(i,1) = b(i);
     %compare values with the unique element: to use logical indexing
     tmp=a(:,1) == b(i);
     c(i,2) = sum(a(tmp,2));
     c(i,3) = sum(tmp);
end


N N schreef:
> Could someone guide me through the following data sorting
> issue please? FYI please: I’ve attached something that
> I’ve tried (based on posts in math works forum) below.
> Thanks in advance!
>
> My data is this:
>
> 2000 0
> 2006 1
> 2004 1
> 2000 1
> 2006 0
> 2006 0
> 2004 1
>
> %column 1 independent variable, column 2 yes/no, yes=1
> (ie, correct) & no=0 (ie, incorrect)
>
> My aim is to get the following:
>
> 2000 1 2
> 2004 2 2
> 2006 1 3
>
> %first column = tested independent variables, 2nd
> column=no of correct responses & 3rd column=total number
> of testing at each independent variable.
> -----------------------------------------------------
> My script so far:
>
> a=[
> 2000 0
> 2006 1
> 2004 1
> 2000 1
> 2006 0
> 2006 0
> 2004 1
> ] %data
>
> b=sortrows(a) %sorts in ascending row
>
> c=unique(b(:,1))%gives me a column of independent
> variables (I am stuck here)

Subject: sorting data

From: us

Date: 14 Apr, 2008 11:40:20

Message: 3 of 4

"N N" :
<SNIP unique accumulation fest...

> 2000 0
> 2006 1
> 2004 1
> 2000 1
> 2006 0
> 2006 0
> 2004 1
> My aim is to get the following:
> 2000 1 2
> 2004 2 2
> 2006 1 3

one of the many solutions

% the data
     a=[
          2000 0
          2006 1
          2004 1
          2000 1
          2006 0
          2006 0
          2004 1
     ];
% the engine
     ua=unique(a(:,1));
     [na,ix]=histc(a(:,1),ua);
     nx=accumarray(ix,a(:,2));
     r=[ua,nx,na];
% the result
     disp(r)
%{
     r =
     2000 1 2
     2004 2 2
     2006 1 3
%}

us

Subject: sorting data

From: N N

Date: 14 Apr, 2008 23:56:02

Message: 4 of 4

Thank you "Kris" & "us" for the codes. I understood how
they work as well. Cheers :)

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
code us 14 Apr, 2008 07:45:06
unique us 14 Apr, 2008 07:45:06
histc us 14 Apr, 2008 07:45:06
accumarray us 14 Apr, 2008 07:45:06
rssFeed for this Thread

Contact us at files@mathworks.com