row ranking among multiple matrices

1 view (last 30 days)
Hi there,
I have three matrix A, B, and C. Here A is a complex matrix of size N*M, where each row of A is generated from the same distribution. Each row of A are also related to corresponding rows of B and C, i.e., the first row will related to the first rows of B and C, so if the row position of A changes, then I want B and C's corresponding rows will be changed as well . Now, calculate the power of each row in A, i.e., the first row power will be: abs(A(1,1))^2+abs(A(1,2))^2+...+abs(A(1,M))^2, then ranking the rows of A based on row's power in descending order, i.e., the highest power row to be the first row, and the lowest poewr row. Meanwhile, I want the rows in B and C can align the ranking changes in A. Is there any efficient way to do this?

Accepted Answer

Bruno Luong
Bruno Luong on 17 Aug 2023
Edited: Bruno Luong on 17 Aug 2023
Just very standard matlab programming
% Generate test data
N=10; M=3;
A=rand(N,M)+1i*rand(N,M);
MB = 1;
MC = 2;
B=randi(10,N,MB)
B = 10×1
7 4 3 5 3 4 6 3 6 6
C=randi(10,N,MC)
C = 10×2
6 3 3 2 8 8 3 6 1 4 1 9 7 3 3 1 10 10 5 2
p = 2; % power
[Anorm,is] = sort(vecnorm(A,p,2),'descend')
Anorm = 10×1
1.8755 1.7151 1.6110 1.5849 1.4104 1.3776 1.2816 1.2354 1.1222 1.0291
is = 10×1
5 1 8 4 7 6 10 9 3 2
A = A(is,:)
A =
0.7121 + 0.3695i 0.4868 + 0.9979i 0.9602 + 0.8479i 0.9269 + 0.4427i 0.5795 + 0.9249i 0.2050 + 0.8081i 0.4052 + 0.9197i 0.9609 + 0.1776i 0.3922 + 0.6903i 0.3646 + 0.2818i 0.1953 + 0.7376i 0.9552 + 0.8972i 0.0714 + 0.7200i 0.3226 + 0.4563i 0.8747 + 0.6232i 0.1924 + 0.3216i 0.5120 + 0.6637i 0.8073 + 0.6346i 0.6943 + 0.0221i 0.5488 + 0.4397i 0.8141 + 0.0525i 0.3099 + 0.1480i 0.4871 + 0.3865i 0.8718 + 0.5114i 0.7629 + 0.4168i 0.2592 + 0.0964i 0.4537 + 0.4703i 0.3264 + 0.1703i 0.5729 + 0.3254i 0.6828 + 0.1528i
B = B(is,:)
B = 10×1
3 7 3 5 6 4 6 6 3 4
C = C(is,:)
C = 10×2
1 4 6 3 3 1 3 6 7 3 1 9 5 2 10 10 8 8 3 2

More Answers (2)

Steven Lord
Steven Lord on 17 Aug 2023
Call sort with two outputs. Use the second output to reorder the other arrays. See the "Sort Vectors in Same Order" example on the sort documentation page; you would need to generalize it to index into matrices rather than vectors, but that's not difficult.

Jon
Jon on 17 Aug 2023
Edited: Jon on 17 Aug 2023
Similar to @Bruno Luong, but since I already coded up example before I saw @Bruno Luong's I will provide it as alternative here
% Make some example data
m = 5;
n = 3;
A = randn(m,n,"like",1+1i)
A =
0.0358 - 1.7015i 0.4671 - 0.2650i 0.6336 - 0.5262i 0.8548 + 0.0416i 0.2163 - 0.9266i 0.6586 - 1.1739i -1.6230 + 0.9340i 0.6844 - 1.0415i -1.2676 - 0.0650i 0.7128 - 0.1322i 1.0431 + 0.6256i -0.1131 + 1.5801i 0.7582 + 0.1192i -0.6561 + 0.3697i -0.5786 + 0.2997i
B = repmat((1:m)',1,m)
B = 5×5
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5
C = repmat(10*(1:m)',1,m)
C = 5×5
10 10 10 10 10 20 20 20 20 20 30 30 30 30 30 40 40 40 40 40 50 50 50 50 50
% Calculate power in each row of A
p = sum(abs(A).^2,2)
p = 5×1
3.8630 3.4495 6.6708 4.5145 1.5809
% Find sort index in descending order
[~,idx] = sort(p,1,'descend');
% Sort the arrays
Asort = A(idx,:)
Asort =
-1.6230 + 0.9340i 0.6844 - 1.0415i -1.2676 - 0.0650i 0.7128 - 0.1322i 1.0431 + 0.6256i -0.1131 + 1.5801i 0.0358 - 1.7015i 0.4671 - 0.2650i 0.6336 - 0.5262i 0.8548 + 0.0416i 0.2163 - 0.9266i 0.6586 - 1.1739i 0.7582 + 0.1192i -0.6561 + 0.3697i -0.5786 + 0.2997i
Bsort = B(idx,:)
Bsort = 5×5
3 3 3 3 3 4 4 4 4 4 1 1 1 1 1 2 2 2 2 2 5 5 5 5 5
Csort = C(idx,:)
Csort = 5×5
30 30 30 30 30 40 40 40 40 40 10 10 10 10 10 20 20 20 20 20 50 50 50 50 50
  2 Comments
Jon
Jon on 17 Aug 2023
Just made a quick edit on the repmat dimensions so that the example matrices would be a little more obvious
mingcheng nie
mingcheng nie on 18 Aug 2023
Really appreciate for your explaination!

Sign in to comment.

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!