Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
linear addressing of matrix row

Subject: linear addressing of matrix row

From: Temu Gautama

Date: 3 May, 2002 04:16:14

Message: 1 of 4

Hi,


I am trying to speed up the computation of pairwise distances, as done
in pdist, and ran into the following problem: I need to assign values
stored in a column vector in one row of a 2D array. This needs to be
done *without* transposing the column vector, since this would slow
down the code. Is there some Matlab magic to do this? Check the
following code:


***


II = randn(1000,5); % 1000 5D vectors
pp = size(II,1);
D_2 = zeros(pp);
for p=1:pp-1
      IND1 = (p+1):pp;
      T1 = sqrt(sum((II(IND1,:)-repmat(II(p,:),pp-p,1)).^2,2));
      D_2(IND1,p) = T1;
      D_2(p,IND1) = T1'; % I need to get rid of this transpose
end


***


This code speeds up pdist, followed by squareform, which would be
 tic,Dlin=pdist(X,'euclid');D=squareform(Dlin);toc


Any ideas?


Temu

Subject: linear addressing of matrix row

From: John D'Errico

Date: 3 May, 2002 06:18:34

Message: 2 of 4

In article <eead524.-1@WebX.raydaftYaTP>, "Temu Gautama"
<temu@neuro.kuleuven.ac.be> wrote:

> I am trying to speed up the computation of pairwise distances, as done
> in pdist, and ran into the following problem: I need to assign values
> stored in a column vector in one row of a 2D array. This needs to be
> done *without* transposing the column vector, since this would slow
> down the code. Is there some Matlab magic to do this? Check the
> following code:

I used Doug Schwarz's genops toolbox

http://home.earthlink.net/~dschwarz2/genops.html

to re-write your interpoint distance computation,
looping over the second dimension of II instead.

II = randn(1000,5); % 1000 5D vectors

tic
pp = size(II,1);
D_2 = zeros(pp);
for p=1:(pp-1)
  IND1 = (p+1):pp;
  T1 = sqrt(sum((II(IND1,:)-repmat(II(p,:),pp-p,1)).^2,2));
  D_2(IND1,p) = T1;
  D_2(p,IND1) = T1'; % I need to get rid of this transpose
end
toc

elapsed_time =
         14.5


% now try it assuming genops was available
genops on
tic
n = size(II,2);
x=II(:,1);
d=(x-x').^2;
for i=2:n
  x=II(:,i);
  d=d+(x-x').^2;
end
d=sqrt(d);
toc

elapsed_time =
       10.949

HTH,
John D'Errico

--

Subject: linear addressing of matrix row

From: barrowes@mediaone.net (Ben Barrowes)

Date: 3 May, 2002 08:16:03

Message: 3 of 4

You can make a mex file out of this to speed it up considerably.
Here are the steps using matlab2fmex
https://sourceforge.net/projects/matlab2fmex/

Here is your function:

function p=foo(l1,l2,outsize)
II = randn(l1,l2); % 1000 5D vectors
pp = size(II,1);
D_2 = zeros(pp);
for p=1:(pp-1)
  IND1 = [(p+1):pp];
  T1 = sqrt(sum((II(IND1,:)-repmat(II(p,:),pp-p,1)).^2,2));
  D_2(IND1,p) = T1;
  D_2(p,IND1) = T1'; % I need to get rid of this transpose
end

In native matlab, this takes:
>> l1=3000;l2=5;t=cputime;a=foo(l1,l2,zeros(l1,l2));cputime-t
ans =
          6.98333333333332
>>

Now, for matlab2fmex, make a mat file for foo.m:
>> l1=300,l2=5
l1 =
   300
l2 =
     5
>> matlab2fmex_save('a=foo(l1,l2,zeros(l1,l2));');

This creates the file foo.mat.
Now convert foo.m into a mex file using Fortran90:

>> matlab2fmex('foo');
Converting --- foo.m ==> foo.f ==> foo.mex
matlab2fmex.
 ==> mex foo.f mexfunctions.f mexoperators.f mexcallback.f
>>

Now foo.m is ready for any l1 and l2. The execution time using foo.mex is:
>> l1=3000;l2=5;t=cputime;a=foo(l1,l2,zeros(l1,l2));cputime-t
ans =
          1.18333333333334
>>

This is a speedup of
>> 6.98333333333332/1.18333333333334
ans =
          5.90140845070418
>>

Hope this helps!
Ben Barrowes




"Temu Gautama" <temu@neuro.kuleuven.ac.be> wrote in message news:<eead524.-1@WebX.raydaftYaTP>...
> Hi,
>
>
> I am trying to speed up the computation of pairwise distances, as done
> in pdist, and ran into the following problem: I need to assign values
> stored in a column vector in one row of a 2D array. This needs to be
> done *without* transposing the column vector, since this would slow
> down the code. Is there some Matlab magic to do this? Check the
> following code:
>
>
> ***
>
>
> II = randn(1000,5); % 1000 5D vectors
> pp = size(II,1);
> D_2 = zeros(pp);
> for p=1:pp-1
> IND1 = (p+1):pp;
> T1 = sqrt(sum((II(IND1,:)-repmat(II(p,:),pp-p,1)).^2,2));
> D_2(IND1,p) = T1;
> D_2(p,IND1) = T1'; % I need to get rid of this transpose
> end
>
>
> ***
>
>
> This code speeds up pdist, followed by squareform, which would be
> tic,Dlin=pdist(X,'euclid');D=squareform(Dlin);toc
>
>
> Any ideas?
>
>
> Temu

Subject: linear addressing of matrix row

From: Shiguo

Date: 9 Apr, 2011 03:42:04

Message: 4 of 4

Hi,

I am also trying to convert .m file to mex file to speed up. I used the method you suggested. But get the following errors:

cl : Command line warning D9024 : unrecognized source file type 'foo.f90', object file assumed
cl : Command line warning D9027 : source file 'foo.f90' ignored
cl : Command line warning D9021 : no action performed
 
  C:\PROGRA~1\MATLAB\R2009A\BIN\MEX.PL: Error: Compile of 'foo.f90' failed.
 
??? Error using ==> mex at 218
Unable to complete successfully.
 

When I use mex -setup, it shows that I only have one complier

mex -setup
Please choose your compiler for building external interface (MEX) files:
 
Would you like mex to locate installed compilers [y]/n? y
 
Select a compiler:
[1] Microsoft Visual C++ 2005 SP1 in C:\Program Files (x86)\Microsoft Visual Studio 8
 
[0] None

I then installed the Visual Fortran Compiler, it still shows only one compiler.

Any suggestions? Thank you.


barrowes@mediaone.net (Ben Barrowes) wrote in message <6440383c.0205030716.6a9724dc@posting.google.com>...
> You can make a mex file out of this to speed it up considerably.
> Here are the steps using matlab2fmex
> https://sourceforge.net/projects/matlab2fmex/
>
> Here is your function:
>
> function p=foo(l1,l2,outsize)
> II = randn(l1,l2); % 1000 5D vectors
> pp = size(II,1);
> D_2 = zeros(pp);
> for p=1:(pp-1)
> IND1 = [(p+1):pp];
> T1 = sqrt(sum((II(IND1,:)-repmat(II(p,:),pp-p,1)).^2,2));
> D_2(IND1,p) = T1;
> D_2(p,IND1) = T1'; % I need to get rid of this transpose
> end
>
> In native matlab, this takes:
> >> l1=3000;l2=5;t=cputime;a=foo(l1,l2,zeros(l1,l2));cputime-t
> ans =
> 6.98333333333332
> >>
>
> Now, for matlab2fmex, make a mat file for foo.m:
> >> l1=300,l2=5
> l1 =
> 300
> l2 =
> 5
> >> matlab2fmex_save('a=foo(l1,l2,zeros(l1,l2));');
>
> This creates the file foo.mat.
> Now convert foo.m into a mex file using Fortran90:
>
> >> matlab2fmex('foo');
> Converting --- foo.m ==> foo.f ==> foo.mex
> matlab2fmex.
> ==> mex foo.f mexfunctions.f mexoperators.f mexcallback.f
> >>
>
> Now foo.m is ready for any l1 and l2. The execution time using foo.mex is:
> >> l1=3000;l2=5;t=cputime;a=foo(l1,l2,zeros(l1,l2));cputime-t
> ans =
> 1.18333333333334
> >>
>
> This is a speedup of
> >> 6.98333333333332/1.18333333333334
> ans =
> 5.90140845070418
> >>
>
> Hope this helps!
> Ben Barrowes
>
>
>
>
> "Temu Gautama" <temu@neuro.kuleuven.ac.be> wrote in message news:<eead524.-1@WebX.raydaftYaTP>...
> > Hi,
> >
> >
> > I am trying to speed up the computation of pairwise distances, as done
> > in pdist, and ran into the following problem: I need to assign values
> > stored in a column vector in one row of a 2D array. This needs to be
> > done *without* transposing the column vector, since this would slow
> > down the code. Is there some Matlab magic to do this? Check the
> > following code:
> >
> >
> > ***
> >
> >
> > II = randn(1000,5); % 1000 5D vectors
> > pp = size(II,1);
> > D_2 = zeros(pp);
> > for p=1:pp-1
> > IND1 = (p+1):pp;
> > T1 = sqrt(sum((II(IND1,:)-repmat(II(p,:),pp-p,1)).^2,2));
> > D_2(IND1,p) = T1;
> > D_2(p,IND1) = T1'; % I need to get rid of this transpose
> > end
> >
> >
> > ***
> >
> >
> > This code speeds up pdist, followed by squareform, which would be
> > tic,Dlin=pdist(X,'euclid');D=squareform(Dlin);toc
> >
> >
> > Any ideas?
> >
> >
> > Temu

Tags for this Thread

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.

Contact us