I am having trouble indexing

1 Ansicht (letzte 30 Tage)
Leeba Ann Chacko
Leeba Ann Chacko am 6 Jun. 2022
I have two 2x4 matrices A and B. I have rearranged A in decending order and have obtained the index for this rearrangement. I would like to use this index to rearrange B. However, when I try that, it is is only working for the 1st column and using the values from this column for the rest. How do I rectify this?
Here is my code:
A = rand(2,4);
B = rand(2,4);
[C,idx] = sort(A,'descend');
D=B(idx);

Akzeptierte Antwort

KSSV
KSSV am 6 Jun. 2022
A = rand(2,4);
B = rand(2,4);
[C,idx] = sort(A,'descend');
D=B;
for i = 1:size(idx,2)
D(:,i) = B(idx(:,i),i) ;
end
  1 Kommentar
Leeba Ann Chacko
Leeba Ann Chacko am 6 Jun. 2022
Thank you, this worked well for me! :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by