Thread Subject: how to further vectorize ?

Subject: how to further vectorize ?

From: Vladimir Stanojevic

Date: 20 Oct, 2009 17:11:19

Message: 1 of 4

Hi,
how this can be further vectorized:
I need to remove from matrix M (example given below) the rows where K-L and I-J are swapped.

Example:
First row gives [K L] = [1 1] and [I J] = [2,2] ---> Row 6 should therefore be removed from M.
Second row gives [K L] = [1 1] and [I J] = [2,3] ---> Row 10 should therefore be removed from M.
Third row gives [K L] = [1 2] and [I J] = [2,3] ---> Row 11 should therefore be removed from M, etc.

I made a program (see below) that does the job, but it is not fully vectorized.

Any suggestion?

Cheers,
v.

Example:
--------
Matrix M:
        K L I J
        1 1 2 2 101.4 128.8 136.7
        1 1 2 3 23.5 29.9 68.6
        1 2 2 3 5.3 22.1 15.4
        1 1 3 3 63.5 80.7 87.6
        1 1 3 4 63.5 80.7 87.6
                                                         
        2 2 1 1 101.3 136.6 128.7
        2 2 1 2 22.8 30.7 95.1
        2 2 3 3 30.7 41.4 42.4
        2 2 3 4 6.6 9.0 22.1
                                                         
        2 3 1 1 23.5 68.5 29.9
        2 3 1 2 5.3 15.4 22.1
        2 3 3 3 7.1 20.8 9.8
                                                         
        3 3 1 1 63.4 87.5 80.6
        3 3 1 2 14.2 19.7 59.5
        3 3 1 3 0.2 0.3 3.0
        3 3 2 2 30.7 42.4 41.4
        3 4 1 1 0.7 2.4 1.4
                                                         
        1 3 6 18 0.0 0.1 0.5
        1 3 7 20 0.0 0.1 0.5
                                                         
        4 5 10 18 0.1 0.3 0.5
       24 3 4 20 0.2 0.4 0.6


Here is my code and I wonder how it can be further vectorized:

for K=1:max(M(:,1));
    IJ=M(M(:,1)==K,3:4);
    if ~isempty(IJ)
        tf = ismember(M(:,1:4),[IJ,K*ones(size(IJ,1),1),M(M(:,1)==K,2)],'rows');
        M(tf,:)=[];
    end
end


RESULT:
--------
   1 1 2 2 101.4 128.8 136.7
   1 1 2 3 23.5 29.9 68.6
   1 2 2 3 5.3 22.1 15.4
   1 1 3 3 63.5 80.7 87.6
   1 1 3 4 63.5 80.7 87.6
   2 2 1 2 22.8 30.7 95.1
   2 2 3 3 30.7 41.4 42.4
   2 2 3 4 6.6 9.0 22.1
   2 3 3 3 7.1 20.8 9.8
   3 3 1 2 14.2 19.7 59.5
   3 3 1 3 0.2 0.3 3.0
   1 3 6 18 0.0 0.1 0.5
   1 3 7 20 0.0 0.1 0.5
   4 5 10 18 0.1 0.3 0.5
  24 3 4 20 0.2 0.4 0.6

Subject: how to further vectorize ?

From: Jan Simon

Date: 20 Oct, 2009 22:09:03

Message: 2 of 4

Dear Vladimir!

> I need to remove from matrix M (example given below) the rows where K-L and I-J are swapped.
> Here is my code and I wonder how it can be further vectorized:
>
> for K=1:max(M(:,1));
> IJ=M(M(:,1)==K,3:4);
> if ~isempty(IJ)
> tf = ismember(M(:,1:4),[IJ,K*ones(size(IJ,1),1),M(M(:,1)==K,2)],'rows');
> M(tf,:)=[];
> end
> end

Do you really need a vectorized method or should it be just faster?
This might be a little bit faster:

 for K = 1:max(M(:,1))
     IV = (M(:, 1) == K);
     if any(IV)
         tf = ismember(M(:,1:4), M(IV, [3, 4, 1, 2]), 'rows');
         M(tf, :) = [];
     end
 end

Is ISMEMBER the main problem? How large is the array?

Good night, Jan

Subject: how to further vectorize ?

From: Vladimir Stanojevic

Date: 21 Oct, 2009 16:04:20

Message: 3 of 4

Dear Jan,
thanks for your reply. Basically, I wanted to speed up the code, and in most cases a vectorization is the best way to do it. At the moment, it takes about 15s to finish the task, depending on the matrix size (which is about 80,000 rows).

Anyway, thanks for your concerns, your code is indeed a little bit faster than mine :)) !!

Cheers,
v.


"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hblcdv$20m$1@fred.mathworks.com>...
> Dear Vladimir!
>
> > I need to remove from matrix M (example given below) the rows where K-L and I-J are swapped.
> > Here is my code and I wonder how it can be further vectorized:
> >
> > for K=1:max(M(:,1));
> > IJ=M(M(:,1)==K,3:4);
> > if ~isempty(IJ)
> > tf = ismember(M(:,1:4),[IJ,K*ones(size(IJ,1),1),M(M(:,1)==K,2)],'rows');
> > M(tf,:)=[];
> > end
> > end
>
> Do you really need a vectorized method or should it be just faster?
> This might be a little bit faster:
>
> for K = 1:max(M(:,1))
> IV = (M(:, 1) == K);
> if any(IV)
> tf = ismember(M(:,1:4), M(IV, [3, 4, 1, 2]), 'rows');
> M(tf, :) = [];
> end
> end
>
> Is ISMEMBER the main problem? How large is the array?
>
> Good night, Jan

Subject: how to further vectorize ?

From: Jan Simon

Date: 21 Oct, 2009 22:15:06

Message: 4 of 4

Dear Vladimir!

> thanks for your reply. Basically, I wanted to speed up the code, and in most cases a vectorization is the best way to do it. At the moment, it takes about 15s to finish the task, depending on the matrix size (which is about 80,000 rows).

If each processing step depends on the previous steps, vectorization is not as straight as for independent steps.

ISMEMBER needs a lot of time for sorting the array again and again. So at first try to pre-sort the array (accelerates the sorting). In addition the inputs are processed with UNIQUE('rows') inside ISMEMBER. If you apply M = UNQIUE(M, 'rows') once, it should be a much faster to copy just the needed lines from ISMEMBER to create your own function avoiding further sorting and unificating.

Good luck, Jan

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
vectorization Vladimir Stanojevic 20 Oct, 2009 13:14:04
rssFeed for this Thread

Contact us at files@mathworks.com