I have two matrices that are related and I need to sort one matrix while sorting the corresponding matrix the same way.
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
James Tursa
2018-3-30
编辑:James Tursa
2018-3-30
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!