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!