MATLAB: Separating Elements of a Matrix with Criteria

  • Context: MATLAB 
  • Thread starter Thread starter dpsguy
  • Start date Start date
  • Tags Tags
    Matlab Matrix
Click For Summary

Discussion Overview

The discussion revolves around the problem of separating elements of a matrix based on a specified criterion, specifically arranging elements less than or equal to 50 at the beginning of each row while maintaining their relative order, and placing elements greater than 50 at the end. Participants explore various approaches to implement this in MATLAB, including algorithm design and the use of built-in functions.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes the desired output format for a given matrix and requests assistance in writing a MATLAB program to achieve this separation.
  • Another participant questions whether the difficulty lies in algorithm creation or MATLAB implementation, suggesting that the problem relates to known sorting algorithms.
  • A participant shares a working solution involving a custom function to separate the elements based on the specified criteria, detailing the algorithm's structure and logic.
  • One participant suggests using MATLAB's built-in sort function, although they express uncertainty about its effectiveness given the requirement to maintain relative order.
  • A participant expresses dissatisfaction with the sort function, indicating it did not meet their needs and opting to use their own solution instead.
  • Another participant proposes a different approach using a loop to concatenate elements based on the criteria, while another later clarifies that sorting is not necessary for elements less than or equal to 50, suggesting a simpler concatenation method instead.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to solve the problem. Multiple methods are proposed, and there is disagreement on the necessity of sorting elements greater than 50.

Contextual Notes

Some participants express confusion regarding the logic of the proposed solutions, indicating a potential gap in understanding MATLAB programming concepts. The discussion also highlights varying interpretations of the problem requirements.

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

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
15K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
1
Views
2K