|
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)
|