row ranking among multiple matrices
1 view (last 30 days)
Show older comments
mingcheng nie
on 17 Aug 2023
Commented: mingcheng nie
on 18 Aug 2023
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?
0 Comments
Accepted Answer
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)
C=randi(10,N,MC)
p = 2; % power
[Anorm,is] = sort(vecnorm(A,p,2),'descend')
A = A(is,:)
B = B(is,:)
C = C(is,:)
More Answers (2)
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.
0 Comments
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)
B = repmat((1:m)',1,m)
C = repmat(10*(1:m)',1,m)
% Calculate power in each row of A
p = sum(abs(A).^2,2)
% Find sort index in descending order
[~,idx] = sort(p,1,'descend');
% Sort the arrays
Asort = A(idx,:)
Bsort = B(idx,:)
Csort = C(idx,:)
2 Comments
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
See Also
Categories
Find more on Logical 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!