Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Sort data by relative order
Replies: 2   Last Post: Mar 17, 2013 5:36 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Nasser Abbasi

Posts: 5,698
Registered: 2/7/05
Re: Sort data by relative order
Posted: Mar 16, 2013 6:18 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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






Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.