# Sort column based on value in another column (sort rows by column values a>b>c)

조회 수: 5(최근 30일)
Nathan Garcia 2021년 3월 11일
댓글: Nathan Garcia 2021년 4월 1일
I am using the function [E,index] = sortrows(A,'descend');
This sorts the first column by row value and also secondarily sorts the second column. I would then like to specify how the sort works based on relative values in a third column. For example, I am only interested in rows where column A value > column B value > column C value. I am interested in the index that identifies the rows that fit this definition. How can I set this up?

댓글을 달려면 로그인하십시오.

### 채택된 답변

Pranav Verma 2021년 3월 15일
편집: Pranav Verma 2021년 3월 16일
Hi Nathan,
From the question I understand that after performing the usual sorting operation on the table, you want the rows which follow the definition of "value of column A > column B > column C".
You can start by sorting the rows in whichever order you want them to be sorted (ascending / descending). After that you can use the find function in MATLAB and get the row numbers which follow the definition provided.
For eg;,
% a = 3 2 1
% 2 3 4
% 5 4 3
% 1 2 3
a = [3 2 1 ; 2 3 4 ; 5 4 3 ; 1 2 3];
% value of column A > column B > column C
b = find(a(:,1) > a(:,2) & a(:,2) > a(:,3));
% b =
% 1
% 3
Here we see that row 1 and 3 follow the given condition specified inside the find function and hence it returns the row numbers of these two rows.
Hope this helps!
Thanks
##### 댓글 수: 1표시숨기기 없음
Nathan Garcia 2021년 4월 1일
Thank you! This is very helpful.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!