# Matrix problem in MATLAB

1. Jul 8, 2009

### dpsguy

I wish to separate the elements of a matrix on the basis of a certain criteria, say, all the elements that are greater than 50 should come at the end, in each row.

For example,
[45 36 78 89 12
54 23 46 71 21
31 41 67 92 19]

should become

[45 36 12 78 89
23 46 21 54 71
31 41 19 67 92]

The important thing here is that the relative order of the elements of a particular type (i.e. those greater or less than 50) should not change. So, in row 2, 54 should still be before 71 after the process is complete.

Assuming that I know the number of elements of each type in each row of the matrix, how do I write a program to accomplish this in MATLAB? I am quite clueless about how to proceed. Any help or suggestions would be most welcome.

2. Jul 8, 2009

### KLoux

Are you having difficulties because you don't know how to create the algorithm, or because you have to do it in MATLAB? If it's the former, I would recommend googling some sorting algorithms. The problem you describe is closely related to some well-known sorting algorithms. If it's the latter, maybe you can describe your process a little bit?

-Kerry

3. Jul 11, 2009

### dpsguy

Well, both actually. Anyway, I found a solution that at least works.

Assuming I have a matrix A of dimensions mxn. The criteria is that all elements less than a certain value ‘len’ should arrange themselves at the beginning of each row. The function separate returns a modified desired matrix A.

Code (Text):

A=separate(A,m,n,len);

function A=separate(A,m,n,len)
c=A;
A(A>len)=0;
c(c<=len)=0;
[A c]=separate2(A,c,m,n);
A(A==0)=c(A==0);

function [A c]=separate2(A,c,m,n)
for i=1:m
p=0;
q=0;
for j=1:n
if A(i,j)==0
p=p+1;
else
if p==0
continue
else
A(i,j-p)=A(i,j);
A(i,j)=0;
end
end
end
for k=n:-1:1
if c(i,k)==0
q=q+1;
else
if q==0
continue
else
c(i,k+q)=c(i,k);
c(i,k)=0;
end
end
end
end

Anyone know a better algorithm or a better way of writing it in MATLAB?

4. Jul 12, 2009

### MATLABdude

Can you use MATLAB's built-in sort function? (Pay particular attention to Example 1):
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sort.html

I'm not sure if all your data sets are so nicely ordered (such that the relative ordering thing you mention is moot) but if they are, that'd be helpful. Otherwise, what you've got is probably as good as you're going to get.

5. Jul 14, 2009

### dpsguy

Thanks for your suggestion, MATLABdude, but I had already tried the sort function and it didn't work out. Guess I’ll have to satisfy myself with my own solution.

6. Jul 17, 2009

### matonski

for i = 1:length(A(:,1))
row = A(i,:);
A(i,:) = [row(row<=50) sort(row(row>50))];​
end

Last edited: Jul 17, 2009
7. Jul 25, 2009

### dpsguy

But thanks a lot for the help, matonski. It really works, though I still can't get my head around understanding why, with my limited knowledge of MATLAB programming.

8. Jul 26, 2009

### matonski

for i = 1:length(A(:,1))
The statement length(A(:,1)) gives the number of rows in A. This loop is going to loop through each of the rows in A.

row = A(i,:);
This is setting row to be the ith row of A

A(i,:) = [row(row<=50) sort(row(row>50))];
This is reassigning the ith row of A. row(row<=50) returns those elements of row that are less than 50. Similarly, sort(row(row>50)) returns those elements of row that are greater than 50, but then sorts the output. The output of both are concatenated into a single array, which is then assigned to the ith row of A.

9. Jul 26, 2009

### matonski

Actually, I just reread your first post. I mistakenly thought you wanted those numbers greater than 50 to be sorted, while the numbers less than 50 to retain their original order. Since that is not the case, and you want to keep the relative orders, you need to remove the sort command in the third line. So the third line should just read

A(i,:) = [row(row<=50) row(row>50)];