Matrix Sortrows by one of it Values

2 views (last 30 days)
Hello Everyone
sorry for this simple question, But i Need answer ASAP,and i'm reall stuck in it
sorry for bad English also i just realized the whole question is wrong:| , but still problem Exist if You Still want to help :
i have a matrix it has about 150 rows in it , also i input a number to a variable,
i want search all of my matrix rows and find closest value to my variable value number i gave ,
i already wrote this part , problem is that first time it finds closest value in my matrix , i want to find second and third value close to my input value,
Thanks , for Your Reply
M.Dadmand

Accepted Answer

Image Analyst
Image Analyst on 27 Jan 2013
Edited: Image Analyst on 27 Jan 2013
Try this:
% Sample data
A =[...
9 0.28
3 0.50
1 0.30
4 0.35
2 0.65
6 0.25]
% Sort rows by how close the second column value is to 0.32.
[~, sortedIndexes] = sortrows(abs(A-0.32), 2);
% Create an output array that is A shuffled by the sort index.
out = A(sortedIndexes, :)
Use [sortedA, sortedIndexes] if your version of MATLAB is so old that it doesn't support the ~ syntax.
If you want to extract just the top 3 rows, use this instead:
out = A(sortedIndexes(1:3), :)
A =
9 0.28
3 0.5
1 0.3
4 0.35
2 0.65
6 0.25
out =
1 0.3
4 0.35
9 0.28
  2 Comments
Mohammad
Mohammad on 27 Jan 2013
Edited: Mohammad on 27 Jan 2013
i think this is the answer, i still need to test it , my Data set is big, just have a question when Indexes get higher it column value gets more far from our input Var to compare and search, Yes?
i'll mark it answered as soon as i'll get sure it 100% works , Thank You so much. <3
Image Analyst
Image Analyst on 27 Jan 2013
It should work no matter what value you're comparing against.

Sign in to comment.

More Answers (3)

Cedric
Cedric on 27 Jan 2013
Edited: Cedric on 27 Jan 2013
You should play a bit with sortrows, just to have a better feeling of how it works. For example
>> A = [2 7 12; 1 3 10; 2 6 13; 1 2 11]
A =
2 7 12
1 3 10
2 6 13
1 2 11
Now I would like to sort this matrix first in ascending order based on column 1, and then (when multiple elements of the first column are identical) in descending order base on column 2:
>> sortrows(A, [1, -2])
ans =
1 3 10
1 2 11
2 7 12
2 6 13
The priority between columns for the sort is defined by elements of the 2nd argument, and the ascending or descending behavior is defined the sign of these elements.
Now your question doesn't seem to make sense, because A(:,7) is a column vector, which means that it has only one column, so it is unclear why you want to sort it according to the value in A(100,7), which should be 1 or -1 for it to work dimension-wise, and which would be sorted with the rest of column 7.
  1 Comment
Cedric
Cedric on 27 Jan 2013
Edited: Cedric on 27 Jan 2013
Here is a bit more information that, I think, will be useful to you after reading your reply to Image Analyst. Say you have a vector that you want to sort according to how close values of its elements are from a given value; you could proceed as follows:
>> a = [1, 9, 4, 2, 13] ; % Vector.
>> v = 5 ; % Given value.
>> [~,ix] = sort(abs(a-v)) ;
>> a(ix)
ans =
4 2 1 9 13
Here you see that a(ix) gives you a list of closest values from closest to farthest. To understand this, look for abs() and sort() in the help. For the latter, you will see that it outputs the index of sorted elements as a 2nd output argument. So what we do here, is that we sort absolute values of differences, and then we index the vector with the index returned by sort(). Most important, decompose the code above to understand what it does, i.e. evaluate the following and see what each expression outputs:
>> abs(a-v)
>> sort(abs(a-v))
>> [s,ix] = sort(abs(a-v))
>> [~,ix] = sort(abs(a-v))

Sign in to comment.


Image Analyst
Image Analyst on 27 Jan 2013
I have no idea what "sort it rows by the value is in A[100,7]" means - perhaps get another English speaker to paraphrase that. The help for sortrows() says this:
B = sortrows(A,column) sorts the matrix based on the columns specified in the vector column. If an element of column is positive, the MATLAB software sorts the corresponding column of matrix A in ascending order; if an element of column is negative, MATLAB sorts the corresponding column in descending order. For example, sortrows(A,[2 -3]) sorts the rows of A first in ascending order for the second column, and then by descending order for the third column.
Is there somethi9ng about that which you do not understand?
  2 Comments
Mohammad
Mohammad on 27 Jan 2013
sorry for bad English also i just realized the whole question is wrong:| , but still problem Exist if You Still want to help :
i have a matrix it has about 150 rows in it , also i input a number to a variable,
i want search all of my matrix rows and find closest value to my variable value number i gave ,
i already wrote this part , problem is that first time it finds closest value in my matrix , i want to find second and third value close to my input value,
Thanks , for Your Reply
Image Analyst
Image Analyst on 27 Jan 2013
See my new answer below.

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 27 Jan 2013
A=randi(10,5,4)
value=4
[n,m]=size(A)
b=abs(A-value)
[jj,jj]=sort(b,2)
ii=repmat((1:n)',1,m)
idx=sub2ind(size(A),ii,jj)
out=A(idx)
  7 Comments
Image Analyst
Image Analyst on 27 Jan 2013
Edited: Image Analyst on 27 Jan 2013
Uh, okay... Azzi - did you understand that? Because I don't. So neither Azzi's nor my script does what you want?

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices 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!