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:
Left division for matrix arrays

Subject: Left division for matrix arrays

From: Mauro

Date: 6 Oct, 2010 14:34:17

Message: 1 of 20

If A and B are both arrays of M nxn matrices - that is, (n,n,M)
matrices - is it possible to calculate the left division of A by B,
without using a for loop like the following?

y=zeros(n,n,M);

for j=1:M
   y(:,:,j)=A(:,:,j)\B(:,:,j);
end

Subject: Left division for matrix arrays

From: Sean

Date: 6 Oct, 2010 14:49:21

Message: 2 of 20

Mauro <msmscarlatti@googlemail.com> wrote in message <9d9dd921-4483-4024-9a44-b2ae257a18e2@j25g2000yqa.googlegroups.com>...
> If A and B are both arrays of M nxn matrices - that is, (n,n,M)
> matrices - is it possible to calculate the left division of A by B,
> without using a for loop like the following?
>
> y=zeros(n,n,M);
>
> for j=1:M
> y(:,:,j)=A(:,:,j)\B(:,:,j);
> end

%You need to play with bsxfun some more!

n = 4;m = 5;
A = repmat(magic(n),[1 1 m]);
B = rand(n,n,m);

bsxfun(@ldivide,A,B)

%SCd

Subject: Left division for matrix arrays

From: James Tursa

Date: 6 Oct, 2010 14:50:42

Message: 3 of 20

Mauro <msmscarlatti@googlemail.com> wrote in message <9d9dd921-4483-4024-9a44-b2ae257a18e2@j25g2000yqa.googlegroups.com>...
> If A and B are both arrays of M nxn matrices - that is, (n,n,M)
> matrices - is it possible to calculate the left division of A by B,
> without using a for loop like the following?
>
> y=zeros(n,n,M);
>
> for j=1:M
> y(:,:,j)=A(:,:,j)\B(:,:,j);
> end

See ndfun from Peter Boettcher's MATLAB Stuff:

http://www.mit.edu/~pwb/matlab/

James Tursa

Subject: Left division for matrix arrays

From: Mauro

Date: 6 Oct, 2010 15:11:44

Message: 4 of 20

On Oct 6, 3:49 pm, "Sean " <sean.dewol...@nospamplease.umit.maine.edu>
wrote:
> Mauro <msmscarla...@googlemail.com> wrote in message <9d9dd921-4483-4024-9a44-b2ae257a1...@j25g2000yqa.googlegroups.com>...
> > If A and B are both arrays of M nxn matrices - that is, (n,n,M)
> > matrices - is it possible to calculate the left division of A by B,
> > without using a for loop like the following?
>
> > y=zeros(n,n,M);
>
> > for j=1:M
> >    y(:,:,j)=A(:,:,j)\B(:,:,j);
> > end
>
> %You need to play with bsxfun some more!
>
> n = 4;m = 5;
> A = repmat(magic(n),[1 1 m]);
> B = rand(n,n,m);
>
> bsxfun(@ldivide,A,B)
>
> %SCd

Thanks

Subject: Left division for matrix arrays

From: Mauro

Date: 6 Oct, 2010 15:13:03

Message: 5 of 20

On Oct 6, 3:50 pm, "James Tursa"
<aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> Mauro <msmscarla...@googlemail.com> wrote in message <9d9dd921-4483-4024-9a44-b2ae257a1...@j25g2000yqa.googlegroups.com>...
> > If A and B are both arrays of M nxn matrices - that is, (n,n,M)

>
> See ndfun from Peter Boettcher's MATLAB Stuff:
>
> http://www.mit.edu/~pwb/matlab/
>
> James Tursa

That seems very interesting, however, I'd rather use native Matlab
code, if possible.

Subject: Left division for matrix arrays

From: James Tursa

Date: 6 Oct, 2010 15:17:26

Message: 6 of 20

Mauro <msmscarlatti@googlemail.com> wrote in message <58866b60-fb93-491c-9bec-35331646379c@d25g2000yqc.googlegroups.com>...
> On Oct 6, 3:50 pm, "James Tursa"
> <aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> > Mauro <msmscarla...@googlemail.com> wrote in message <9d9dd921-4483-4024-9a44-b2ae257a1...@j25g2000yqa.googlegroups.com>...
> > > If A and B are both arrays of M nxn matrices - that is, (n,n,M)
>
> >
> > See ndfun from Peter Boettcher's MATLAB Stuff:
> >
> > http://www.mit.edu/~pwb/matlab/
> >
> > James Tursa
>
> That seems very interesting, however, I'd rather use native Matlab
> code, if possible.

OK. My advice would be to only use native MATLAB code that works, however. Be sure to test your results.

James Tursa

Subject: Left division for matrix arrays

From: Sean

Date: 6 Oct, 2010 15:24:05

Message: 7 of 20


> OK. My advice would be to only use native MATLAB code that works, however. Be sure to test your results.
>
> James Tursa

That is very important to note here:
bsxfun() is doing a left division not solving the matrix. That would be mldivide which is not supported for bsxfun.

Subject: Left division for matrix arrays

From: James Tursa

Date: 6 Oct, 2010 15:53:28

Message: 8 of 20

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i8i4al$83h$1@fred.mathworks.com>...
>
> > OK. My advice would be to only use native MATLAB code that works, however. Be sure to test your results.
> >
> > James Tursa
>
> That is very important to note here:
> bsxfun() is doing a left division not solving the matrix. That would be mldivide which is not supported for bsxfun.

Yes ... I was waiting to see if OP was going to be able to discover this problem on his own :)

James Tursa

Subject: Left division for matrix arrays

From: Mauro

Date: 6 Oct, 2010 17:14:17

Message: 9 of 20

On Oct 6, 4:53 pm, "James Tursa"
<aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> "Sean " <sean.dewol...@nospamplease.umit.maine.edu> wrote in message <i8i4al$83...@fred.mathworks.com>...
>
> > That is very important to note here:
> > bsxfun() is doing a left division not solving the matrix.  That would be mldivide which is not supported for bsxfun.
>
> Yes ... I was waiting to see if OP was going to be able to discover this problem on his own :)
>
> James Tursa

Thanks.. I decided to try and use ndfun after all, but I can't get it
to work. The mex file provided on Peter Boettcher's site doesn't work,
and when I try to compile the file ndfun.c using "mex ndfun.c" I get
an error:

??? Error using ==> mex at 221
Unable to complete successfully.

How can I fix this?

Thanks,

Mauro

Subject: Left division for matrix arrays

From: Mauro

Date: 6 Oct, 2010 17:17:52

Message: 10 of 20

On Oct 6, 6:14 pm, Mauro <msmscarla...@googlemail.com> wrote:

> Thanks.. I decided to try and use ndfun after all, but I can't get it
> to work. The mex file provided on Peter Boettcher's site doesn't work,
> and when I try to compile the file ndfun.c using "mex ndfun.c" I get
> an error:
>
> ??? Error using ==> mex at 221
> Unable to complete successfully.
>
> How can I fix this?
>
> Thanks,
>
> Mauro

I forgot to mention: I am using Matlab R2009b, on a PC with Intel Core
2 Duo CPU, running Windows XP Professional.

Subject: Left division for matrix arrays

From: Mauro

Date: 6 Oct, 2010 17:21:00

Message: 11 of 20

To be exact, the error I am getting in the compilation is as follows

Creating library C:\DOCUME~1\HOME\LOCALS~1\TEMP\MEX_EM~1\templib.x and
object C:\DOCUME~1\HOME\LOCALS~1\TEMP\MEX_EM~1\templib.exp
ndfun.obj : error LNK2019: unresolved external symbol _dgecon
referenced in function _compute_lu
ndfun.obj : error LNK2019: unresolved external symbol _dgetrf
referenced in function _compute_lu
ndfun.obj : error LNK2019: unresolved external symbol _dgeev
referenced in function _mexFunction
ndfun.obj : error LNK2019: unresolved external symbol _dgetri
referenced in function _mexFunction
ndfun.obj : error LNK2019: unresolved external symbol _dgetrs
referenced in function _mexFunction
ndfun.obj : error LNK2019: unresolved external symbol _zgemm
referenced in function _mexFunction
ndfun.obj : error LNK2019: unresolved external symbol _dgemm
referenced in function _mexFunction
ndfun.mexw32 : fatal error LNK1120: 7 unresolved externals

Subject: Re:cheap cars

From: bouchermark654

Date: 6 Oct, 2010 17:35:39

Message: 12 of 20

Well nice question..do anyone have the answer.


[url=http://www.sexyeditor.com]cheap cars[/url]

Subject: Left division for matrix arrays

From: James Tursa

Date: 6 Oct, 2010 17:59:19

Message: 13 of 20

Mauro <msmscarlatti@googlemail.com> wrote in message <790d88f2-605d-4fd4-b8cd-2ee56e139c8f@g18g2000yqk.googlegroups.com>...
> On Oct 6, 6:14 pm, Mauro <msmscarla...@googlemail.com> wrote:
>
> > Thanks.. I decided to try and use ndfun after all, but I can't get it
> > to work. The mex file provided on Peter Boettcher's site doesn't work,
> > and when I try to compile the file ndfun.c using "mex ndfun.c" I get
> > an error:
> >
> > ??? Error using ==> mex at 221
> > Unable to complete successfully.
> >
> > How can I fix this?
> >
> > Thanks,
> >
> > Mauro
>
> I forgot to mention: I am using Matlab R2009b, on a PC with Intel Core
> 2 Duo CPU, running Windows XP Professional.

You need to link in the BLAS and LAPACK libraries also. Do the following (assuming you are using the provided lcc compiler):

lib_blas = [matlabroot '\extern\lib\win32\lcc\libmwblas.lib'];
lib_lapack = [matlabroot '\extern\lib\win32\lcc\libmwlapack.lib'];
mex('ndfun.c',lib_blas,lib_lapack)

James Tursa

Subject: Left division for matrix arrays

From: Mauro

Date: 6 Oct, 2010 18:17:17

Message: 14 of 20

On Oct 6, 6:59 pm, "James Tursa"
<aclassyguy_with_a_k_not_...@hotmail.com> wrote:

> You need to link in the BLAS and LAPACK libraries also. Do the following (assuming you are using the provided lcc compiler):
>
> lib_blas = [matlabroot '\extern\lib\win32\lcc\libmwblas.lib'];
> lib_lapack = [matlabroot '\extern\lib\win32\lcc\libmwlapack.lib'];
> mex('ndfun.c',lib_blas,lib_lapack)
>
> James Tursa

Thanks, I was able to compile ndfun eventually. Alas, it doesn't
support complex matrices, so I cannot use it. Do you know any other
way to implement mldivide for matrix arrays?

Subject: Left division for matrix arrays

From: Sean

Date: 6 Oct, 2010 18:37:05

Message: 15 of 20

Mauro <msmscarlatti@googlemail.com> wrote in message <aed41844-486f-406f-8c2e-0833cd4ef712@f25g2000yqc.googlegroups.com>...
> On Oct 6, 6:59 pm, "James Tursa"
> <aclassyguy_with_a_k_not_...@hotmail.com> wrote:
>
> > You need to link in the BLAS and LAPACK libraries also. Do the following (assuming you are using the provided lcc compiler):
> >
> > lib_blas = [matlabroot '\extern\lib\win32\lcc\libmwblas.lib'];
> > lib_lapack = [matlabroot '\extern\lib\win32\lcc\libmwlapack.lib'];
> > mex('ndfun.c',lib_blas,lib_lapack)
> >
> > James Tursa
>
> Thanks, I was able to compile ndfun eventually. Alas, it doesn't
> support complex matrices, so I cannot use it. Do you know any other
> way to implement mldivide for matrix arrays?

Well what is so bad about for-loops? When properly implemented they're pretty quick.

Subject: Left division for matrix arrays

From: James Tursa

Date: 6 Oct, 2010 18:47:20

Message: 16 of 20

Mauro <msmscarlatti@googlemail.com> wrote in message <aed41844-486f-406f-8c2e-0833cd4ef712@f25g2000yqc.googlegroups.com>...
> On Oct 6, 6:59 pm, "James Tursa"
> <aclassyguy_with_a_k_not_...@hotmail.com> wrote:
>
> > You need to link in the BLAS and LAPACK libraries also. Do the following (assuming you are using the provided lcc compiler):
> >
> > lib_blas = [matlabroot '\extern\lib\win32\lcc\libmwblas.lib'];
> > lib_lapack = [matlabroot '\extern\lib\win32\lcc\libmwlapack.lib'];
> > mex('ndfun.c',lib_blas,lib_lapack)
> >
> > James Tursa
>
> Thanks, I was able to compile ndfun eventually. Alas, it doesn't
> support complex matrices, so I cannot use it. Do you know any other
> way to implement mldivide for matrix arrays?

I don't know of anything else you could use for the nD case in one step in general. You would have to modify ndfun to handle the complex case. There is an FEX submission for small size matrices up to 3x3 by Bruno Luong:

http://www.mathworks.com/matlabcentral/fileexchange/27762

James Tursa

Subject: Left division for matrix arrays

From: Mauro Scarlatti

Date: 6 Oct, 2010 19:30:23

Message: 17 of 20

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i8ig7o$nri$1@fred.mathworks.com>...

> I don't know of anything else you could use for the nD case in one step in general. You would have to modify ndfun to handle the complex case. There is an FEX submission for small size matrices up to 3x3 by Bruno Luong:
>
> http://www.mathworks.com/matlabcentral/fileexchange/27762

Thanks. Looks like it might be useful.

Subject: Left division for matrix arrays

From: Mauro Scarlatti

Date: 6 Oct, 2010 19:33:05

Message: 18 of 20

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i8ifkh$dpm$1@fred.mathworks.com>...
 
> > Thanks, I was able to compile ndfun eventually. Alas, it doesn't
> > support complex matrices, so I cannot use it. Do you know any other
> > way to implement mldivide for matrix arrays?
>
> Well what is so bad about for-loops? When properly implemented they're pretty quick.

Is there a more efficient way other than the obvious one:

for j=1:M
   y(:,:,j)=A(:,:,j)\B(:,:,j);
end

I tried this, but it was rather slow.

Subject: Left division for matrix arrays

From: Sean

Date: 6 Oct, 2010 20:17:05

Message: 19 of 20

> Is there a more efficient way other than the obvious one:
>
> for j=1:M
> y(:,:,j)=A(:,:,j)\B(:,:,j);
> end
>
> I tried this, but it was rather slow.

Preallocate y, i.e.
%size(A) = size(B) = size(y)
%
y = zeros(size(A));
for ii = 1:M
 y(:,:,ii) = A(:,:,ii)\B(:,:,ii);
end

Subject: Left division for matrix arrays

From: Bruno Luong

Date: 7 Oct, 2010 12:23:03

Message: 20 of 20

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i8ilg1$gdr$1@fred.mathworks.com>...
> > Is there a more efficient way other than the obvious one:
> >
> > for j=1:M
> > y(:,:,j)=A(:,:,j)\B(:,:,j);
> > end
> >
> > I tried this, but it was rather slow.
>
> Preallocate y, i.e.
> %size(A) = size(B) = size(y)
> %
> y = zeros(size(A));
> for ii = 1:M
> y(:,:,ii) = A(:,:,ii)\B(:,:,ii);
> end

You won't be able to accelerate much. Try this:
http://www.mathworks.com/matlabcentral/fileexchange/24260-multiple-same-size-linear-solver

Bruno

Tags for this Thread

No tags are associated with 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