Sort a matrix according to only one column [duplicate]












1
















This question already has an answer here:




  • Sorting entire matrix according to one column in matlab

    2 answers




I have this matrix A,



A= [10  -12  4  8;
1 3 11 -2;
1 -9 8 0;
1 1 9 3].


if I use the sortrows function in Matlab B = sortrows(A). I will get this.



B = [1    -9     8     0;
1 1 9 3;
1 3 11 -2;
10 -12 4 8].


The answer I wanted is this.



B = [1   3     11     -2; 
1 -9 8 0;
1 1 9 3;
10 -12 4 8]


The thing is, I want to sort my rows here but only based on column 1. if values in column 1 are same, then don't sort them according to column 2(which in genreal this function perform).



I really appreciate, if anyone can help me with this.



Thank you.










share|improve this question















marked as duplicate by obchardon, Community Nov 21 '18 at 8:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    1
















    This question already has an answer here:




    • Sorting entire matrix according to one column in matlab

      2 answers




    I have this matrix A,



    A= [10  -12  4  8;
    1 3 11 -2;
    1 -9 8 0;
    1 1 9 3].


    if I use the sortrows function in Matlab B = sortrows(A). I will get this.



    B = [1    -9     8     0;
    1 1 9 3;
    1 3 11 -2;
    10 -12 4 8].


    The answer I wanted is this.



    B = [1   3     11     -2; 
    1 -9 8 0;
    1 1 9 3;
    10 -12 4 8]


    The thing is, I want to sort my rows here but only based on column 1. if values in column 1 are same, then don't sort them according to column 2(which in genreal this function perform).



    I really appreciate, if anyone can help me with this.



    Thank you.










    share|improve this question















    marked as duplicate by obchardon, Community Nov 21 '18 at 8:27


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      1












      1








      1









      This question already has an answer here:




      • Sorting entire matrix according to one column in matlab

        2 answers




      I have this matrix A,



      A= [10  -12  4  8;
      1 3 11 -2;
      1 -9 8 0;
      1 1 9 3].


      if I use the sortrows function in Matlab B = sortrows(A). I will get this.



      B = [1    -9     8     0;
      1 1 9 3;
      1 3 11 -2;
      10 -12 4 8].


      The answer I wanted is this.



      B = [1   3     11     -2; 
      1 -9 8 0;
      1 1 9 3;
      10 -12 4 8]


      The thing is, I want to sort my rows here but only based on column 1. if values in column 1 are same, then don't sort them according to column 2(which in genreal this function perform).



      I really appreciate, if anyone can help me with this.



      Thank you.










      share|improve this question

















      This question already has an answer here:




      • Sorting entire matrix according to one column in matlab

        2 answers




      I have this matrix A,



      A= [10  -12  4  8;
      1 3 11 -2;
      1 -9 8 0;
      1 1 9 3].


      if I use the sortrows function in Matlab B = sortrows(A). I will get this.



      B = [1    -9     8     0;
      1 1 9 3;
      1 3 11 -2;
      10 -12 4 8].


      The answer I wanted is this.



      B = [1   3     11     -2; 
      1 -9 8 0;
      1 1 9 3;
      10 -12 4 8]


      The thing is, I want to sort my rows here but only based on column 1. if values in column 1 are same, then don't sort them according to column 2(which in genreal this function perform).



      I really appreciate, if anyone can help me with this.



      Thank you.





      This question already has an answer here:




      • Sorting entire matrix according to one column in matlab

        2 answers








      matlab sorting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 15:49









      obchardon

      3,8221820




      3,8221820










      asked Nov 20 '18 at 15:34









      Ketan SahuKetan Sahu

      154




      154




      marked as duplicate by obchardon, Community Nov 21 '18 at 8:27


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by obchardon, Community Nov 21 '18 at 8:27


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          4














          Sort the first column only with the function sort keep only the index, then use this index to sort the whole matrix.



          A= [10  -12  4  8;
          1 3 11 -2;
          1 -9 8 0;
          1 1 9 3];
          [~,ind] = sort(A(:,1))
          B = A(ind,:)


          or simply use the second argument of the function sortrows which precise the column sorting vector:



          B = sortrows(A,1) 





          share|improve this answer


























          • Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)

            – Ketan Sahu
            Nov 20 '18 at 15:56




















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Sort the first column only with the function sort keep only the index, then use this index to sort the whole matrix.



          A= [10  -12  4  8;
          1 3 11 -2;
          1 -9 8 0;
          1 1 9 3];
          [~,ind] = sort(A(:,1))
          B = A(ind,:)


          or simply use the second argument of the function sortrows which precise the column sorting vector:



          B = sortrows(A,1) 





          share|improve this answer


























          • Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)

            – Ketan Sahu
            Nov 20 '18 at 15:56


















          4














          Sort the first column only with the function sort keep only the index, then use this index to sort the whole matrix.



          A= [10  -12  4  8;
          1 3 11 -2;
          1 -9 8 0;
          1 1 9 3];
          [~,ind] = sort(A(:,1))
          B = A(ind,:)


          or simply use the second argument of the function sortrows which precise the column sorting vector:



          B = sortrows(A,1) 





          share|improve this answer


























          • Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)

            – Ketan Sahu
            Nov 20 '18 at 15:56
















          4












          4








          4







          Sort the first column only with the function sort keep only the index, then use this index to sort the whole matrix.



          A= [10  -12  4  8;
          1 3 11 -2;
          1 -9 8 0;
          1 1 9 3];
          [~,ind] = sort(A(:,1))
          B = A(ind,:)


          or simply use the second argument of the function sortrows which precise the column sorting vector:



          B = sortrows(A,1) 





          share|improve this answer















          Sort the first column only with the function sort keep only the index, then use this index to sort the whole matrix.



          A= [10  -12  4  8;
          1 3 11 -2;
          1 -9 8 0;
          1 1 9 3];
          [~,ind] = sort(A(:,1))
          B = A(ind,:)


          or simply use the second argument of the function sortrows which precise the column sorting vector:



          B = sortrows(A,1) 






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 15:48

























          answered Nov 20 '18 at 15:43









          obchardonobchardon

          3,8221820




          3,8221820













          • Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)

            – Ketan Sahu
            Nov 20 '18 at 15:56





















          • Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)

            – Ketan Sahu
            Nov 20 '18 at 15:56



















          Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)

          – Ketan Sahu
          Nov 20 '18 at 15:56







          Hello, @Obchardon, Thank you so much, It'sworking, really appreciate your effort. Thank you :)

          – Ketan Sahu
          Nov 20 '18 at 15:56







          Popular posts from this blog

          How to send String Array data to Server using php in android

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Is anime1.com a legal site for watching anime?