MATLAB: Separating Elements of a Matrix with Criteria

  • Context: MATLAB 
  • Thread starter Thread starter dpsguy
  • Start date Start date
  • Tags Tags
    Matlab Matrix
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
8 replies · 6K views
dpsguy
Messages
69
Reaction score
0
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.
 
Physics news on Phys.org
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
 
KLoux said:
Are you having difficulties because you don't know how to create the algorithm, or because you have to do it in MATLAB?

-Kerry

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:
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?
 
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.
 
How about this:

for i = 1:length(A(:,1))
row = A(i,:);
A(i,:) = [row(row<=50) sort(row(row>50))];​
end
 
Last edited:
Sorry for the late reply.

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.
 
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.
 
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)];