row ranking among multiple matrices
조회 수: 2 (최근 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!