Quantcast
Channel: MATLAB Central Newsreader Recent Posts
Viewing all articles
Browse latest Browse all 19628

Re: Sort data by relative order

$
0
0
On 3/16/2013 3:30 PM, Joshua wrote:
> Hi all,
>
> This is probably an easy question, but I've had a hard time thinking of how to do this
>efficiently. Basically, I had individual users perform a task multiple times, and have
>sample data below defined as:
>
> Column 1 = User number
> Column 2 = Test Number (Overall)
>

>
> 1 3 1
> 1 4 2
...
> 1 10 8
> 13 15 1
> 13 16 2
> 13 17 3
> 13 18 4
> 13 19 5

starting with your suffled version of your original data , but I
added an extra 3rd column:

-----------------------
B =
     48 41 0
     13 18 0
     14 36 0
      1 4 0
      1 10 0
     14 35 0
      1 3 0
      1 5 0
     13 19 0
     13 22 0
     48 42 0
     14 37 0
      1 7 0
     48 38 0
     13 20 0
     48 39 0
      1 9 0
      1 6 0
     13 17 0
     48 40 0
     13 21 0
     13 23 0
     14 34 0
     48 43 0
     13 15 0
      1 8 0
     13 16 0
-------------------------

Then one way:

-------------------

B = sortrows(B)
U = unique(B(:,1));
for i = 1:length(U)
     idx = find(B==U(i));
     B(idx,3) = 1:length(idx)
end
------------------

B =

      1 3 1
      1 4 2
      1 5 3
      1 6 4
      1 7 5
      1 8 6
      1 9 7
      1 10 8
     13 15 1
     13 16 2
     13 17 3
     13 18 4
     13 19 5
     13 20 6
     13 21 7
     13 22 8
     13 23 9
     14 34 1
     14 35 2
     14 36 3
     14 37 4
     48 38 1
     48 39 2
     48 40 3
     48 41 4
     48 42 5
     48 43 6


--Nasser

Viewing all articles
Browse latest Browse all 19628

Trending Articles