Clear Filters
Clear Filters

I have two matrices that are related and I need to sort one matrix while sorting the corresponding matrix the same way.

2 views (last 30 days)
Hi again,
I have two matrices, A and B. A is failure times and B is censoring (all 0s and 1s). I need to sort the failure times (A) from smallest to largest but I need to keep the parts that are censored corresponding.
I need to sort both matrices with the exact same shift so all pieces stay connected and they get sorted smallest to largest.
For example, if I had:
A = [3, 5, 2, 7, 8;
2, 3, 7, 3, 4;
7, 3, 2, 6, 2];
B = [1, 0, 1, 1, 0;
1, 1, 1, 0, 1;
0, 1, 1, 0, 1];
sort(A) = [2, 3, 2, 3, 2;
3, 3, 2, 6, 4;
7, 5, 7, 7, 8]; %sorted by columns
and I need then,
B = [1, 1, 1, 0, 1;
1, 1, 1, 0, 1;
0, 0, 1, 1, 0]; %corresponding to the right censoring/failing category for sort(A)
I then need help making a new vector, C, which would rank sort(A) like this:
A = [3, 5, 2, 7, 8;
2, 3, 7, 3, 4;
7, 3, 2, 6, 2];
C = [1, 1, 1, 1, 1;
2, 2, 2, 2, 2;
3, 3, 3, 3, 3];
(just label the smallest as 1, the second smallest as 2, ..., the largest as n (where n is the total number of units in A).
Can someone help?

Accepted Answer

James Tursa
James Tursa on 30 Mar 2018
Edited: James Tursa on 30 Mar 2018
E.g.,
>> A = [3, 5, 2, 7, 8;
2, 3, 7, 3, 4;
7, 3, 2, 6, 2];
>> B = [1, 0, 1, 1, 0;
1, 1, 1, 0, 1;
0, 1, 1, 0, 1];
>> [SA,I] = sort(A)
SA =
2 3 2 3 2
3 3 2 6 4
7 5 7 7 8
I =
2 2 1 2 3
1 3 3 3 2
3 1 2 1 1
>> J = 0:size(A,2)-1
J =
0 1 2 3 4
>> x = I + J*size(A,1) % or x = bsxfun(@plus,I,J*size(A,1))
x =
2 5 7 11 15
1 6 9 12 14
3 4 8 10 13
>> SB = B(x)
SB =
1 1 1 0 1
1 1 1 0 1
0 0 1 1 0
>> C = repmat((1:size(A,1))',1,size(A,2))
C =
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!