Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Minor disappointment with Matlab
Date: Tue, 8 Sep 2009 18:37:04 +0000 (UTC)
Organization: Boeing
Lines: 33
Message-ID: <h8688g$bm8$1@fred.mathworks.com>
References: <25f0b0e8-fa6b-4283-81eb-d3bfebeb4a97@c37g2000yqi.googlegroups.com> <37a033b3-1375-4593-b673-460ea2713401@x37g2000yqj.googlegroups.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1252435024 11976 172.30.248.35 (8 Sep 2009 18:37:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 8 Sep 2009 18:37:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:569047


Dan <dnp037@yahoo.com> wrote in message <37a033b3-1375-4593-b673-460ea2713401@x37g2000yqj.googlegroups.com>...
>
>  I only used "cross" because I was lazy and did not want to type
> out the expression. Now, maybe I have been naive for many years, and
> the same is true for most matrix/vector functions such as "dot", or
> "norm". Or maybe because the majority of my experiences have been with
> vectorized code, I had not noticed this phenomenon before. In any
> case, I shall be very careful when using Matlab functions when the
> code is not vectorized.

I wouldn't call using the cross function lazy ... if a function is already available I would likely make the same assumption that you did, that it was reasonably coded and not worth my time to hand-code myself.

But dot, in particular, is a function I tend to avoid. In testing I have done, it uses a different algorithm (a simple loop) than a straight matrix multiply and is slower and somewhat less accurate. e.g.

>> a=rand(20000000,1)+rand(20000000,1)*i;
>> b=rand(20000000,1)+rand(20000000,1)*i;
>> format long
>> a'*b
ans =
     9.996446745265679e+006 -1.467960263183340e+003i
>> dot(a,b)
ans =
     9.996446745268498e+006 -1.467960262848279e+003i
>> tic;a'*b;toc
Elapsed time is 0.302599 seconds.
>> tic;dot(a,b);toc
Elapsed time is 0.774087 seconds.

Although it is not obvious from what I have posted, I ran similar calculations against a 100 decimal digit accurate calculation and the a'*b result was the more accurate of the two. Admittedly, the difference is in the trailing bits that your calculations shouldn't depend on, but it probably takes away one of the only reasons I would use dot in the first place since it is slower than a matrix multiply. I would probably only use dot in multi-dimensional cases and even then only if I couldn't easily reformulate it into a matrix multiply.

norm I have found to be pretty fast.

James Tursa