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

2 ビュー (過去 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?

採用された回答

James Tursa
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 件)

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by