row ranking among multiple matrices
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Bruno Luong
2023-8-17
编辑:Bruno Luong
2023-8-17
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,:)
更多回答(2 个)
Steven Lord
2023-8-17
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 个评论
Jon
2023-8-17
编辑:Jon
2023-8-17
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 个评论
Jon
2023-8-17
Just made a quick edit on the repmat dimensions so that the example matrices would be a little more obvious
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!