Sort array elements in ascending order

13 visualizzazioni (ultimi 30 giorni)
Hello,
I have an input array which i want to sort column elements in ascending order
i =
[4 35;
2 4;
2 6 ;
1 9;
4 3 ;
4 6 ;
1 16;
3 17];
and get the sorted output array in the following manner
res=
[1 9;
1 16;
2 4;
2 6;
3 17;
4 3;
4 6;
4 35];
First sort according to the 1st column(ascending order),if the first column is same then check for the second column ,then sort in the acending order based on the second value.
For example : I have 1 as the least value in the first column and have two entries for 1. So check for the second element for 1. I have 9 and 16 as the second element for 1 respectively.Now sort these second elemnts in ascending order.so i have the output as [1 9; 1 16] .
Similary for all the other elements of the matrix.
Please let me know the MATLAB function for sorting the array elements and get me the required output as mentioned above.
Looking forward to hear from you
Thanks Pankaja

Risposta accettata

Stephen23
Stephen23 il 18 Feb 2015
Modificato: Stephen23 il 18 Feb 2015
You can sort the rows and extract the minimum value of each "group" like this:
>> A = [8 4; 3 6; 2 7; 1 4; 2 3;2 1;3 1; 3 5; 8 6; 8 1];
>> B = sortrows(A);
>> X = [true;diff(B(:,1))>0];
>> B(X,:)
ans =
1 4
2 1
3 1
8 1
Where each row is the "minimum" of each group.
  4 Commenti
Pankaja Tanjore
Pankaja Tanjore il 19 Feb 2015
Hello,
Thanks for the reply. I am able to get the required result.
I am having one more query .
I have two sorted arrays , whose dimensions are different. for example : matrix A has the dimension 4x2 A=[1 4; 2 1; 3 1; 8 1];
and Matrix B has the dimension 5x2.
B=[1 6; 2 1; 2 5; 3 0; 8 1]; When i find the difference between these two matrcies using C=A-B i am getting the following error
"Matrix dimensions must agree"
So please let me know the function which hlps me to solve this problem
Looking forward to hear from you Thanks Pankaja
Stephen23
Stephen23 il 19 Feb 2015
Modificato: Stephen23 il 19 Feb 2015
If you define exactly what the "difference" between two differently-dimensioned matrices is, then I will show you how to code it.
Your matrices have a different number of rows: what are we supposed to subtract the un-matched row from? Lets look at your matrices in detail:
A - B = C
[1 4; [1 6; = [0 -2;
2 1; 2 1; 0 0;
3 1; 2 5; 1 -4;
8 1] 3 0; 5 1;
8 1] <- what are we supposed to subtract this from?

Accedi per commentare.

Più risposte (1)

Ilham Hardy
Ilham Hardy il 18 Feb 2015
res = sortrows(i,[1,2]);
PS: Don't name you variable i, by the way.
i in matlab represent sqrt(-1)
  1 Commento
Pankaja Tanjore
Pankaja Tanjore il 18 Feb 2015
Hello , Thanks for the reply. As suggested by you i used the function sortrows() and I am getting the correct output. I tested the function with the following input data.
Input_Arr=[8 4; 3 6; 2 7; 1 4; 2 3;2 1;3 1; 3 5; 8 6; 8 1];
i am getting the following output Output_Arr=[1 4; 2 1; 2 3; 2 7; 3 1; 3 5 ;3 6; 8 1; 8 4; 8 6];
From this output now i want extract the minimum row data for each column
for example: In the above output array i have [2 1; 2 3; 2 7] for the second row. Now from this i want to extract only [2 1] as that is the minimum of the three.
So please let me know the function in MATLAB to extract this.
Looking forward to hear from you. Thanks Pankaja

Accedi per commentare.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by