MATLAB: Separating Elements of a Matrix with Criteria

In summary, The purpose of "MATLAB: Separating Elements of a Matrix with Criteria" is to separate elements of a matrix based on a specific criteria. This function can be used by providing a matrix and desired criteria as inputs, and it can handle various types of criteria and multi-dimensional matrices. It is a built-in function in MATLAB for data analysis and manipulation.
  • #1
dpsguy
69
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
  • #2
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
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?
 
  • #4
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
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
How about this:

for i = 1:length(A(:,1))
row = A(i,:);
A(i,:) = [row(row<=50) sort(row(row>50))];​
end
 
Last edited:
  • #7
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.
 
  • #8
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
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)];
 

What is the purpose of "MATLAB: Separating Elements of a Matrix with Criteria"?

The purpose of this function is to separate the elements of a matrix based on a specific criteria, such as a certain value or condition. This can be useful for data analysis and manipulation in MATLAB.

How do you use "MATLAB: Separating Elements of a Matrix with Criteria"?

To use this function, you first need to have a matrix in MATLAB. Then, you can provide the matrix and the desired criteria as inputs to the function. The function will then return a new matrix with the elements that meet the criteria.

What types of criteria can be used in "MATLAB: Separating Elements of a Matrix with Criteria"?

This function can use a variety of criteria, such as numerical values, logical conditions, and even custom functions. It depends on the specific requirements of the data and the desired outcome.

Can "MATLAB: Separating Elements of a Matrix with Criteria" handle multi-dimensional matrices?

Yes, this function can handle multi-dimensional matrices in MATLAB. It will separate the elements based on the criteria in each dimension, resulting in a new multi-dimensional matrix.

Is "MATLAB: Separating Elements of a Matrix with Criteria" a built-in function in MATLAB?

Yes, this function is a built-in function in MATLAB and can be accessed without any additional installations. It is part of the core functionality of MATLAB for data analysis and manipulation.

Similar threads

Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • General Discussion
Replies
18
Views
11K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • Quantum Physics
Replies
11
Views
1K
Back
Top