From: "Steven Lord" <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Why is sparse * 3D full array allowed?
Date: Fri, 16 Oct 2009 09:52:40 -0400
Organization: The MathWorks, Inc.
Lines: 42
Message-ID: <hb9tq3$mrg$>
References: <hb948j$28q$> <hb99j6$rje$>
Reply-To: "Steven Lord" <>
X-Trace: 1255701123 23408 (16 Oct 2009 13:52:03 GMT)
NNTP-Posting-Date: Fri, 16 Oct 2009 13:52:03 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
X-RFC2646: Format=Flowed; Original
Xref: comp.soft-sys.matlab:577838

"Jan Simon" <> wrote in message 
> Dear James!
>> I noticed that if a sparse matrix is multiplied by a full nD matrix that 
>> can be squeezed to a 2D matrix, the multiplication takes place by doing 
>> the squeeze. But the same calculation for a full matrix is not allowed. 
>> Anybody know why this behavior should be different for sparse vs full? 
>> Seems like it should work the same both ways, either both allow it or 
>> both not allow it.
> There is no reason why a multiplication of SPARSE matrices should accept 
> not matching dimensions! This is mathematically wrong.
>  x = rand(2, 3); y = rand(3, 1, 4);
>  x * y  => error
>  sparse(x) * y  => works
> One could argument, that this behaviour should be valid for FULL arrays 
> also.
> But as Bruno pointed out, even worse:
>  x = rand(2, 3); y = rand(3, 2, 2);
>  sparse(x) * y  => works: ans = full [3 x 4] array
>  sparse(x) * sparse(y) => error
> It looks like The MathWorks uses mxGetM and mxGetN to check the dimensions 
> for the multiplication of SPARSE and FULL arrays. For FULL*FULL and 
> SPARSE*SPARSE the complete check of mxGetDimensions is performed.
> This bug was present since at least Matlab 5.3 (I've removed Matlab 4 for 
> unknown reasons). In my opinion it is worth to break with the backward 
> compatibility here...

Yes, the fact that attempting to perform matrix multiplication with a full 
n-D array and a sparse matrix does not error is a bug.  Thanks to James for 
calling this out.  I've reported it to our development staff.

Steve Lord
comp.soft-sys.matlab (CSSM) FAQ: