Convert column of data from cumulative to relative values

I have what I think is a relatively simple question that I haven't been able to figure out the answer to. I had different users complete the same test multiple times, and have 2 columns of data (A and B) in matrix 'TestData' as below. Column A is a user number and Column B is the cumulative test number (how many TOTAL test have been taken)
Col A Col B
1 1
1 2
1 3
1 4
1 5
2 6
2 7
2 8
3 9
3 10
3 11
3 12
3 13
3 14
I would like to be able to create another column, column C, that converts the cumulative test number to a number that is relative to a given user. In other words, I want column C to show the test number WITHIN a particular users data, as below.
Col A Col B Col C
1 1 1
1 2 2
1 3 3
1 4 4
1 5 5
2 6 1
2 7 2
2 8 3
3 9 1
3 10 2
3 11 3
3 12 4
3 13 5
3 14 6
Any help would be appreciated, I've been stuck on this for awhile and was wondering if anyone has a straightforward way to approach this. Thanks in advance.
JD

 Accepted Answer

test=[1 1 1 1 1 2 2 2 3 3 3 3 3 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14]'
idx=diff([0; find([diff(test(:,1)); 1])])
new_test=[test cell2mat(arrayfun(@(x) (1:x)',idx,'un',0))]

1 Comment

Perfect, I made a few tweaks and it was good to go. Thanks Azzi

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!