How to locate number from a matrix

1 view (last 30 days)
Kaijia Chen
Kaijia Chen on 30 Nov 2021
Commented: KSSV on 30 Nov 2021
I have matrix list2, it's a 526x2 double. Suppose the first column represents the ID of people, and the second column represents the total amount of films they shot. I want to find out the N most prolific films shot by the people. I used sort(list2(:,2),'descend') to rearrange the second colomn to get the most movie shot to the least. However, I also need to determine the people's ID, so the question is how to relocate back to the person's ID if we have the information of the second column?

Accepted Answer

KSSV
KSSV on 30 Nov 2021
If idx is the indices of the locations in second column...to get the respective first column just use:
iwant = list2(idx,1)
  5 Comments
Kaijia Chen
Kaijia Chen on 30 Nov 2021
So I rearrange the second column because I wanna find the people in descending order. The problem is how to relocate the first column in the same order? Thank you!
KSSV
KSSV on 30 Nov 2021
A = rand(10,2) ;
[val,idx] = sort(A(:,2)) ;
iwant = A(idx,1)

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!