-5

I have a matrix like below-

x=[1 1 1 1 1;
   2 1 1 1 0;
   3 3 1 0 0;
   3 2 2 0 0];

But i want to make this matrix like-

 x=[2 2 3 0 0;
    1 3 3 0 0;
    1 1 1 2 0;
    1 1 1 1 1];

I have already tried with "sort ascending" but then '0' will come first but i want to keep "0" on last and make one matrix where the number of '1's' will be from low to high throughout the matrix.I am trying but cannot do so. I need Matlab experts help.

  • 1
    I really don't understand the question. What does the *the number of '1's' will be from low to high throughout the matrix* mean? How is the example matrix you want to obtain sorted? – Marc Claesen Aug 14 '13 at 11:54
  • 2
    Your initial matrix 4-by-5 but the result you are looking for is 5-by-5 what is the exact process? Is there more than sorting involved? Are you sorting in both dimensions? – Mohsen Nosratinia Aug 14 '13 at 11:57
  • the number of '1's' will be from low to high throughout the matrix mean-in my example number of 1 in 2nd row =1 ,number of 1 in 3rd row =1 ,number of 1 in 4th row =3,number of 1 in 5th row =5. – user2682286 Aug 14 '13 at 11:59
  • I could be wrong, but it looks like you're in the process of writing linear algebra code to reduce a matrix to RREF. If this is the case, someone may be able to help with the methodology [here](http://math.stackexchange.com/). – Danzomida Aug 14 '13 at 12:14
  • @user2682286 Please have a look at my edit. Your example data is ambiguous. I have illustrated the difference between my answer and Moshen's but it's not clear from your question which one you actually want. But please do have a look and understand this ambiguity as it could prevent you having bugs later on. – Dan Aug 14 '13 at 12:59
  • If you [look here](http://stackoverflow.com/questions/18151296/) you'll find solutions (with respect to rows instead of columns) - including some that do it "without sort" as you asked. – horchler Aug 14 '13 at 13:24

1 Answers1

5

To get the zeros at the end, set them to infinity (inf), sort and then set them back to zero

x=[1 1 1 1 1;
   2 1 1 1 0;
   3 3 1 0 0;
   3 2 2 0 0]

x(x == 0) = inf;
y = sort(x, 2, 'ascend');
y(y==inf) = 0;

Now count the number of 1s per row and reorder from least to most

[~, I] = sort(sum(y==1,2));

y(I, :)

ans =

   2   2   3   0   0
   1   3   3   0   0
   1   1   1   2   0
   1   1   1   1   1

EDIT:

The example data is ambiguous. Take this as the input:

x=[1 1 1 1 1;
   2 1 1 1 0;
   7 1 0 1 0;
   3 3 1 0 0;
   3 2 2 0 0];

Now the answers by Moshen (i.e. to just sort each column after sorting the rows) return

x =

   2   3   7   0   0
   1   2   3   0   0
   1   1   3   0   0
   1   1   1   2   0
   1   1   1   1   1

Whereas mine returns

ans =

   2   2   3   0   0
   1   3   3   0   0
   1   1   7   0   0
   1   1   1   2   0
   1   1   1   1   1

Mine preserves the row integrity. But it is not clear which answer the OP is after.

Dan
  • 45,079
  • 17
  • 88
  • 157
  • for reordering I'd call `flipud`. – fpe Aug 14 '13 at 12:02
  • 1
    @fpe who says the `1`s are in the reverse order to begin with? – Dan Aug 14 '13 at 12:02
  • I only meant to say that after `y(y==Inf) = 0;`, I'd call `flipud(y)` to get the expected result. – fpe Aug 14 '13 at 12:08
  • @fpe my point is that that only works in this specific case because in the OPs input matrix the number of `1`s per row in the matrix was descending but that was not given as an assumption so this is more generalized. – Dan Aug 14 '13 at 12:11
  • @Dan, yes u r correct.Atfirst i run this code for smaller matrix then it was right but afterthat when i run this code for large matrix then it shows different results.ur result is correct. – user2682286 Aug 14 '13 at 13:04
  • The title of the question is "...without using sort". – horchler Aug 14 '13 at 13:25
  • 1
    @horchler True, but in the post he mentions that he tried using `sort` but failed. I would say the requirements are unclear at best. – Dennis Jaheruddin Aug 14 '13 at 14:01